lrobbo314
Well-known Member
- Joined
- Jul 14, 2008
- Messages
- 3,963
- Office Version
- 365
- Platform
- 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.
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.
HXRGB
I wrote this one to convert a hex string to RGB values.
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.
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 | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
A | B | C | D | E | F | G | H | |||
1 | R | G | B | |||||||
2 | 0 | 0 | 0 | 7.192099 | Happy Halloween | |||||
3 | 255 | 103 | 0 | |||||||
4 | ||||||||||
5 | ||||||||||
6 | Text | 17, 42, 70 | Example | Contrast | Good Contrast | |||||
7 | Background | 172, 200, 229 | 8.415388931 | |||||||
8 | ||||||||||
9 | ||||||||||
10 | Text | 31, 189, 34 | Example | Contrast | Bad Contrast | |||||
11 | Background | 187, 172, 230 | 1.214846767 | |||||||
Sheet1 |
Cell Formulas | ||
---|---|---|
Range | Formula | |
F2 | F2 | =CONTRAST(A2:C2,A3:C3) |
A2:C3 | A2 | =SPLITBYDELIMITER(Color(D2,2),", ",) |
B6 | B6 | =HXRGB("112a46") |
B7 | B7 | =HXRGB("acc8e5") |
D7,D11 | D7 | =CONTRAST(SPLITBYDELIMITER(B6,", ",),SPLITBYDELIMITER(B7,", ",)) |
B10 | B10 | =HXRGB("1fbd22") |
B11 | B11 | =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.
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