Base: add drawrectangle, getshorthash

This commit is contained in:
Jonathan Jenne 2023-07-27 16:10:49 +02:00
parent 7b2b37a870
commit de418bcca4
2 changed files with 33 additions and 2 deletions

View File

@ -2,10 +2,20 @@
Public Class GraphicsEx Public Class GraphicsEx
Public Shared Function GetBrightness(c As Color) As Single ''' <summary>
Return (c.R * 0.299F + c.G * 0.587F + c.B * 0.114F) / 256.0F ''' 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 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 Public Shared Function GetContrastedColor(pOtherColor As Color) As Color
If GetBrightness(pOtherColor) < 0.55 Then If GetBrightness(pOtherColor) < 0.55 Then
Return Color.White Return Color.White
@ -13,4 +23,21 @@ Public Class GraphicsEx
Return Color.Black Return Color.Black
End If End If
End Function 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 End Class

View File

@ -107,6 +107,10 @@ Public Class StringEx
Return GetChecksum(pStringToCheck) Return GetChecksum(pStringToCheck)
End Function End Function
Public Shared Function GetShortHash(pStringToCheck As String) As String
Return GetChecksum(pStringToCheck).Substring(0, 32)
End Function
Friend Class InvalidChars Friend Class InvalidChars
Public Shared Filenames As String = Regex.Escape(New String(IO.Path.GetInvalidFileNameChars())) Public Shared Filenames As String = Regex.Escape(New String(IO.Path.GetInvalidFileNameChars()))
Public Shared Paths As String = Regex.Escape(New String(IO.Path.GetInvalidPathChars())) Public Shared Paths As String = Regex.Escape(New String(IO.Path.GetInvalidPathChars()))