Imports System.Drawing
Public Class GraphicsEx
'''
''' Returns the brightness of a color as a number between 0 and 1
'''
''' The color to check
''' Low values for dark colors, high values for bright colors.
Public Shared Function GetBrightness(pColor As Color) As Single
Return (pColor.R * 0.299F + pColor.G * 0.587F + pColor.B * 0.114F) / 256.0F
End Function
'''
''' Returns a foreground/text color of either black or white, depending on the brightness of `pOtherColor`
'''
''' The Background color whose brightness is determined
''' A text color which is either white or black
Public Shared Function GetContrastedColor(pOtherColor As Color) As Color
If GetBrightness(pOtherColor) < 0.55 Then
Return Color.White
Else
Return Color.Black
End If
End Function
Public Sub DrawRectangle(Bounds As Rectangle)
Dim oContext As IntPtr
oContext = NativeMethods.GetDC(IntPtr.Zero)
Try
Dim g As Graphics
g = Graphics.FromHdc(oContext)
Try
g.DrawRectangle(Pens.Red, Bounds)
Finally
g.Dispose()
End Try
Finally
NativeMethods.ReleaseDC(IntPtr.Zero, oContext)
End Try
End Sub
End Class