How can I calculate GetBrightness ny myself?

  • Thread starter Thread starter Dominic
  • Start date Start date
D

Dominic

I know there is a function named GetBrightness() on .net framework but if I
need to write this function by myself, how can I do?

Because some language does not provide this function, can I calculate by
using R G B of the color?

Thanks
 
Hi Dominic,

Using Lutz Roeder's Reflector, I see the following code under
System.Drawing.Color:

''' <summary>Gets the hue-saturation-brightness (HSB) brightness value for
this <see cref="T:System.Drawing.Color"></see> structure.</summary>
''' <returns>The brightness of this <see
cref="T:System.Drawing.Color"></see>. The brightness ranges from 0.0 through
1.0, where 0.0 represents black and 1.0 represents white.</returns>
Public Function GetBrightness() As Single
Dim single1 As Single = (CSng(Me.R) / 255!)
Dim single2 As Single = (CSng(Me.G) / 255!)
Dim single3 As Single = (CSng(Me.B) / 255!)
Dim single4 As Single = single1
Dim single5 As Single = single1
If (single2 > single4) Then
single4 = single2
End If
If (single3 > single4) Then
single4 = single3
End If
If (single2 < single5) Then
single5 = single2
End If
If (single3 < single5) Then
single5 = single3
End If
Return ((single4 + single5) / 2!)
End Function


MSDN article for more info:
http://msdn2.microsoft.com/en-us/library/system.drawing.color.getbrightness.aspx

Hope this information helps,

Chris
 
Back
Top