44 lines
1.5 KiB
VB.net
44 lines
1.5 KiB
VB.net
Imports System.Drawing
|
|
|
|
Public Class GraphicsEx
|
|
|
|
''' <summary>
|
|
''' Returns the brightness of a color as a number between 0 and 1
|
|
''' </summary>
|
|
''' <param name="pColor">The color to check</param>
|
|
''' <returns>Low values for dark colors, high values for bright colors.</returns>
|
|
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
|
|
|
|
''' <summary>
|
|
''' Returns a foreground/text color of either black or white, depending on the brightness of `pOtherColor`
|
|
''' </summary>
|
|
''' <param name="pOtherColor">The Background color whose brightness is determined</param>
|
|
''' <returns>A text color which is either white or black</returns>
|
|
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
|