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
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
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
Equal values of r,g and b will produce only shades of gray. Are you looking for a contrast between two shades of gray only or you want color contrast?
 
Upvote 0
I generate r g b values based on the text in a cell, by doing some mathematical calculations on the ascii codes of the characters in the cell. SO that same text has same colors. I need the background color of the cell to be in contrast to the r g b values generated, so that the text is readable.
 
Upvote 0
I generate r g b values based on the text in a cell, by doing some mathematical calculations on the ascii codes of the characters in the cell. SO that same text has same colors. I need the background color of the cell to be in contrast to the r g b values generated, so that the text is readable.
Are the r g b values you generate always such that r=g=b or can r g and b have different values for the some text?
 
Upvote 0
here is how I generate RGB
you can use anything, some color that is based on the text


here is what i used
before the next cll i initialize myred, mygre,myblu to zero, i tried adding 40 to red for green, and 100 to red for blue but i can end up with colors near grey anytime.
<code>
prcsss = Left(cll.Value, InStr(cll.Value, " :") - 1)
For i = 1 To Len(prcsss)
myred = Asc(LCase(Left(prcsss, Len(prcsss) - i + 1))) + myred
mygre = mygre + Asc(LCase(Left(prcsss, Len(prcsss) - i + 1))) * Asc(LCase(Left(prcsss, Len(prcsss) - i + 1)))
myblu = myblu + Asc(LCase(Left(prcsss, Len(prcsss) - i + 1)))
Next i



I just converted the RGB to HSL using circledchicken's link. rotated H by 360 and converted back to RGB, WIll post my results in a bit.</code>
 
Last edited:
Upvote 0
Hi,

Perhaps try adapting the code provided in this article to VBA:
How to Calculate a Complementary Colour / Color | Convert RGB to HSL and HSL to RGB


I converted the entire code to VBA as posted on EasyRGB. The contrasth function inverts the "H" in HSL. but the HSL2RGB function does not even look at H if s=0, so what is the point of inverting H as described in the site. What am I doing wrong?



Code:
Function RGB2HSL(r, g, b) As Variant
var_R = (r / 255)  '                     //RGB from 0 to 255
var_G = (g / 255)
var_B = (b / 255)
Set wf = Application.WorksheetFunction


var_Min = wf.Min(var_R, var_G, var_B)  '    //Min. value of RGB
var_Max = wf.Max(var_R, var_G, var_B)   '   //Max. value of RGB
del_Max = var_Max - var_Min           '  //Delta RGB value


L = (var_Max + var_Min) / 2


If (del_Max = 0) Then                     ' //This is a gray, no chroma...


   h = 0                              '  //HSL results from 0 to 1
   S = 0


Else                                   ' //Chromatic data...
   If (L < 0.5) Then
   S = del_Max / (var_Max + var_Min)  '
   Else
   S = del_Max / (2 - var_Max - var_Min)
   End If
   del_R = (((var_Max - var_R) / 6) + (del_Max / 2)) / del_Max
   del_G = (((var_Max - var_G) / 6) + (del_Max / 2)) / del_Max
   del_B = (((var_Max - var_B) / 6) + (del_Max / 2)) / del_Max
   If (var_R = var_Max) Then
   h = del_B - del_G
   ElseIf (var_G = var_Max) Then
   h = (1 / 3) + del_R - del_B
   ElseIf (var_B = var_Max) Then
   h = (2 / 3) + del_G - del_R
    End If
   If (h < 0) Then h = h + 1
   If (h > 1) Then h = h - 1
End If
RGB2HSL = Array(h, S, L)
End Function

Function hsl2rgb(h, S, L)
If (S = 0) Then  '                       //HSL from 0 to 1


   r = L * 255                     ' //RGB results from 0 to 255
   g = L * 255
   b = L * 255
Else


   If (L < 0.5) Then
     var_2 = L * (1 + S)
   Else
     var_2 = (L + S) - (S * L)
   End If
     var_1 = 2 * L - var_2


   r = 255 * Hue_2_RGB(var_1, var_2, h + (1 / 3))
   g = 255 * Hue_2_RGB(var_1, var_2, h)
   b = 255 * Hue_2_RGB(var_1, var_2, h - (1 / 3))
End If
   hsl2rgb = RGB(r, g, b)
End Function
Function Hue_2_RGB(v1, v2, vH)              ' //Function Hue_2_RGB


   If (vH < 0) Then vH = vH + 1
   If (vH > 1) Then vH = vH - 1
   If ((6 * vH) < 1) Then
   Hue_2_RGB = (v1 + (v2 - v1) * 6 * vH)
   Exit Function
   End If
   If ((2 * vH) < 1) Then
   Hue_2_RGB = (v2)
   Exit Function
   End If
   If ((3 * vH) < 2) Then
   Hue_2_RGB = (v1 + (v2 - v1) * ((2 / 3) - vH) * 6)
   Exit Function
   End If
Hue_2_RGB = v1
End Function


Function contrasth(h As Variant)
h = h + 0.5
If h > 1 Then h = h - 1
contrasth = h
End Function
 
Upvote 0
The code that i used to call these functions is as below, but grey background returns grey text

Code:
Option Base 1

Sub ColorActiveCell()
Call colorer(ActiveCell, 127, 127, 127)
End Sub



Sub colorer(cl As Range, r As Integer, g As Integer, b As Integer)cl.Interior.Color = RGB(r, g, b)
myarr = RGB2HSL(r, g, b)
h = contrasth(myarr(1))
S = myarr(2)
L = myarr(3)
inverted = hsl2rgb(h, S, L)
cl.Font.Color = inverted
End Sub
 
Upvote 0
Here is a function I have posted in the past...

Code:
Function TextColorToUse(BackColor As Long) As Long
  TextColorToUse = -vbWhite * (77 * (BackColor Mod &H100) + 151 * ((BackColor \ &H100) _
                   Mod &H100) + 28 * ((BackColor \ &H10000) Mod &H100) < 32640)
End Function

If you have your values as separate RGB values, then just use the RGB function as the argument. For example...

Code:
R = 012
G = 123
B = 234
NewFontColor = TextColorToUse(RGB(R, G, B))
 
Upvote 0

Forum statistics

Threads
1,214,875
Messages
6,122,042
Members
449,063
Latest member
ak94

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