How dark is a color

P

Paul

How do I ascertain how dark a selected color is ?

I want to amend the font color of a form to complement the backcolor
selected by the user - i.e. white font on dark backgrounds, black font on
light backgrounds
 
R

RB Smissaert

This function seems to work very well:

Function GetContrastingFont(lClr As Long) As Long

'returns a contrasting font colour, given a long colour
'------------------------------------------------------
Dim R As Long
Dim G As Long
Dim B As Long

B = Int(lClr / 65536)
G = Int((lClr Mod 65536) / 256)
R = Int(lClr Mod 256)

'calculation from Peter Thornton
'-------------------------------
If R * 0.206 + G * 0.679 + B * 0.115 > 135 Then
GetContrastingFont = vbBlack
Else
GetContrastingFont = vbWhite
End If

End Function


RBS
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top