CONTRAST

=CONTRAST(range,range)

color1
3 cell horizontal range containing red, green, and blue values in that order respectively
color2
3 cell horizontal range containing red, green, and blue values in that order respectively

CONTRAST returns the contrast ration between 2 colors. The range of values returned is from 0-21 with 21 being the highest contrast value.

lrobbo314

Well-known Member
Joined
Jul 14, 2008
Messages
3,963
Office Version
  1. 365
Platform
  1. Windows
CONTRAST returns the contrast ratio between 2 colors. The range of values returned is from 1-21 with 21 being the highest contrast value.

Excel Formula:
=LAMBDA(
    color1,
    color2,
        LET(
            a,color1,
            b,color2,
            d,.05,
            f,LAMBDA(rgb,SUM(MAP(rgb/255,LAMBDA(v,IF(v<0.03928,v/12.92,((v+0.055)/1.055)^2.4)))*{0.2126,0.7152,0.0722})),
            x,f(a),
            y,f(b),
                (MAX(x,y)+d)/(MIN(x,y)+d)
        )
)

CONTRAST
ABCDEFGH
1RGB
20007.192099Happy Halloween
32551030
4
5
6Text17, 42, 70ExampleContrastGood Contrast
7Background172, 200, 2298.415388931
8
9
10Text31, 189, 34ExampleContrastBad Contrast
11Background187, 172, 2301.214846767
Sheet1
Cell Formulas
RangeFormula
F2F2=CONTRAST(A2:C2,A3:C3)
A2:C3A2=SPLITBYDELIMITER(Color(D2,2),", ",)
B6B6=HXRGB("112a46")
B7B7=HXRGB("acc8e5")
D7,D11D7=CONTRAST(SPLITBYDELIMITER(B6,", ",),SPLITBYDELIMITER(B7,", ",))
B10B10=HXRGB("1fbd22")
B11B11=HXRGB("bbace6")
Dynamic array formulas.


Extras:

Some of the other formulas in the table above include:

SPLITTEXTBYDELIMITER

This function is credited to @Xlambda. It is his ATEXTSPLIT function that I wrote over and over again to figure it out. (y)

Excel Formula:
=LAMBDA(
    text,delim,shrink,
        LET(h,"Æ",d,"ᐃ",
        a,SUBSTITUTE(text,delim,h),
        b,IF(shrink,a,SUBSTITUTE(SUBSTITUTE(TRIM(SUBSTITUTE(SUBSTITUTE(a," ",d),h," "))," ",h),d," ")),
        n,LEN(b)-LEN(SUBSTITUTE(b,h,""))+1,
        c,MAX(n),sc,SEQUENCE(,c),
        x,SEARCH(d,SUBSTITUTE(h&b,h,d,sc)),
        y,SEARCH(d,SUBSTITUTE(b&h,h,d,sc)),
            IFERROR(MID(b,x,y-x),"")
        )
)

HXRGB

I wrote this one to convert a hex string to RGB values.

Excel Formula:
=LAMBDA(
    text,
        MAP(text,
            LAMBDA(h,TEXTJOIN(", ",,HEX2DEC(MID(h,{1;3;5},2))))
        )
)

COLOR

This is a VBA function that I got from the forum. Can't remember where it came from to give credit. There are a bunch of versions of this around. This function returns the color of the background of a cell. It will return HEX, RGB, COLORINDEX, or COLOR, depending on the formatType argument.

VBA Code:
Function Color(rng As Range, Optional formatType As Integer = 0) As Variant
    Dim colorVal As Variant
    colorVal = rng.Cells(1, 1).Interior.Color
    Select Case formatType
        Case 1
            Color = WorksheetFunction.Dec2Hex(colorVal, 6)
        Case 2
            Color = (colorVal Mod 256) & ", " & ((colorVal \ 256) Mod 256) & ", " & (colorVal \ 65536)
        Case 3
            Color = rng.Cells(1, 1).Interior.ColorIndex
        Case Else
            Color = colorVal
    End Select
End Function
 
Upvote 0

Forum statistics

Threads
1,224,837
Messages
6,181,255
Members
453,028
Latest member
letswriteafairytale

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