RGB contrast COLOR Formula

Arul.rajesh

Active Member
Joined
Sep 20, 2011
Messages
285
Need help to calculate the contrasting colors for given set of RGB values.

For a
<code>
sub contrast()
r=127
g=127
b=127
activecell.font.color=RGB(r,g,b)
new_r=255-r
new_g=255-g
new_b=255-b
activecell.interior.color=RGB(new_r,new_g,new_b)
end sub
</code>
does not help when r,g,b values near 127 or 128 as they are more or less grey.

I need a formula that would give me values of new_r, new_g and new_b that would be in contrast with RGB(r,g,b) for any values of r,g,b from 0-255
 
Thread is a bit old ... Rick's function very good..even elegant
. Some ideas

Why all this 255 - red etc when XOR is available to do the job

Ra.Font.Color = Col
Ra.Interior.Color = Col Xor &HFFFFFF

However if you use many many fonts Excel complains with the error
too many fonts please close some workbooks
some of the below may be useful

Code:
Public Declare Function ColorHLSToRGB& Lib "shlwapi.dll" _
                                       (ByVal wHue&, ByVal wLuminance&, ByVal wSaturation&)
Public Declare Function ColorRGBToHLS& Lib "shlwapi.dll" (ByVal col&, h&, l&, s&)
'
Function ColPerLumSat&(wC&, PP%)
    Dim h&, l&, s&
    ColorRGBToHLS wC, h, l, s    'using l s   .. hue is color s leave it be   
    If PP < 0 Then
        l = l * PP \ 100
        s = s * PP \ 1100
    Else
        l = l + PP * (240 - l) \ 100
        s = s + PP * (200 - l) \ 1100
    End If
    ColPerLumSat = ColorHLSToRGB(h, l, s)
End Function


Function RndPaleC&(Optional r% = 150, Optional g% = 170, Optional b% = 140)
    RndPaleC = rgb(r + Rnd() * (250 - r), g + Rnd() * (255 - g), b + Rnd() * (250 - g))
End Function


' use like ColPerLumSat(rgbBlue,20) ' 20 for dark 80 for light 20 to 92 step 12 for 7 bands or step 4 for 19 bands

' RndPale is useful try it with a variety of r g b

to get contrast some rotate the Hue by 180 degrees of the color wheel HH = (HH + 128) mod 255 .. if 0..255 model

HOWEVER these leave the problem still leave the problem of TOO MANY FONTS as there are 16777216 colors

what we need is a function similar the BlackOrWhite font pickers but to select from a bigger but limited set
say rgbDarkBlue RgbGreen, RgbTomato etc ... say about 8 or 9 choices so that we use only 8 or 9 fonts

Any one got one

More Junk

the number of colors is
&HFFFFFF in Hexadecimal base 16
7 is a lucky number especially on 3 reel pokies 777
there are 6 hexadecimal digits in as color three 6's are 6^3=216
so the number of colors is 16777216

Not as interesting number as 153 ( google 153 )

More junk

a termite went into a pub and asked where is the bar tender
 
Upvote 0

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
I've just found this very useful function from 2011 Rick and it's just what I was looking for.

Thanks.
 
Upvote 0
Thanks Rick for another excellent contribution

It in HLS is ' in a 0 to 240 HLS as from

Public Declare Function ColorRGBToHLS& Lib "shlwapi.dll" (ByVal clrRGB&, pwHue&, pwLuminance&, pwSaturation&)
Public Declare Function ColorHLSToRGB& Lib "shlwapi.dll" (ByVal wHue&, ByVal wLuminance&, ByVal wSaturation&)

if L < 130 then L=240 else L=0
H=160
S=0
for those working in HLS rather than RGB
 
Upvote 0
with the exception L=120 =>> 0 and a few in range (130 .. 190 ) but not as good as Rick's from RGB
 
Upvote 0

Forum statistics

Threads
1,215,482
Messages
6,125,061
Members
449,206
Latest member
Healthydogs

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top