ADOBE AFTER EFFECTS: Forum Expressions Tutorials Creative Cloud

# Expression-based Contrast Ratio Calculator

FAQ   •   VIEW ALL Expression-based Contrast Ratio Calculator on Jun 28, 2020 at 11:26:40 pm

Hi! I'm trying to write an expression that'll take two hex color codes (for now I just have it grabbing them from text layers), determine their Relative Luminances, and then compare them to determine the Contrast Ratio between them.

The part that's tripping me up- I'm pretty sure- is converting After Effects's sRGB 2.2 values to linear RGB, since I keep getting the wrong Relative Luminance. Can anybody tell where I'm screwing things up in this expression?

```//Relative luminance of color 1 txt1 = thisComp.layer("Color1NoTag").text.sourceText; hex1 = parseInt(txt1,16); r1 = hex1 >> 16; g1 = (hex1 & 0x00ff00) >> 8; b1 = hex1 & 0xff; if ((r1/255) &lt;= 0.03928) { rConv1 = (r1/255)/12.92; } if ((g1/255) &lt;= 0.03928) { gConv1 = (g1/255)/12.92; } if ((b1/255)&lt;= 0.03928) { bConv1 = (b1/255)/12.92; } if ((r1/255) > 0.03928) { rConv1 = ((r1/255)+0.055)^2.4; } if ((g1/255) > 0.03928) { gConv1 = ((g1/255)+0.055)^2.4; } if ((b1/255) > 0.03928) { bConv1 = ((b1/255)+0.055)^2.4; } rel1 = ((rConv1/255)*0.2126)+((gConv1/255)*0.7152)+((bConv1/255)*0.0722); //Relative luminance of color 2 txt2 = thisComp.layer("Color2NoTag").text.sourceText; hex2 = parseInt(txt2,16); r2 = hex2 >> 16; g2 = (hex2 & 0x00ff00) >> 8; b2 = hex2 & 0xff; if ((r2/255) &lt;= 0.03928) { rConv2 = (r2/255)/12.92; } if ((g2/255) &lt;= 0.03928) { gConv2 = (g2/255)/12.92; } if ((b2/255)&lt;= 0.03928) { bConv2 = (b2/255)/12.92; } if ((r2/255) > 0.03928) { rConv2 = ((r2/255)+0.055)^2.4; } if ((g2/255) > 0.03928) { gConv2 = ((g2/255)+0.055)^2.4; } if ((b2/255) > 0.03928) { bConv2 = ((b2/255)+0.055)^2.4; } rel2 = ((rConv2/255)*0.2126)+((gConv2/255)*0.7152)+((bConv2/255)*0.0722); //Contrast Ratio (Math.round((rel1+0.05)/(rel2+0.05)*100)/100) +":1"``` Re: Expression-based Contrast Ratio Calculatoron Jun 29, 2020 at 11:08:04 am

Short answer is that ^ as a power-function is not supported in expressions.
Either use Math.pow(x,2.4) or in the new javascript-Engine you can use x**2.4

Also, in the contrast-formula, the brightest color should always be the numerator, see below

In the new engine I'd re-write the whole thing like this:

```function sRGBtoLinear(rgba) { for (i=0; i&lt;3; i++) { u=rgba[i]; if (u&lt;=0.04045) { u = u/12.92; } else { u = 1.055*(u**(1/2.4)) - 0.055; } rgba[i]=u; } return rgba; } function rgb_to_y(rgba) { return 0.2126*rgba + 0.7152*rgba + 0.0722*rgba; } txt1 = thisComp.layer("Color1NoTag").text.sourceText.value; txt2 = thisComp.layer("Color2NoTag").text.sourceText.value; rgb1 = hexToRgb(txt1); rgb2 = hexToRgb(txt2); l1=sRGBtoLinear(rgb1); l2=sRGBtoLinear(rgb2); y1=rgb_to_y(l1); y2=rgb_to_y(l2); ((Math.max(y1,y2)+0.05)/(Math.min(y1,y2)+0.05)).toFixed(2) +":1"``` Re: Expression-based Contrast Ratio Calculatoron Jun 29, 2020 at 11:15:30 am

Sigh… don't forget to fix the "& l t ;" instance to be the lesser-than symbol.
Still not fixed on this site after 19 years… Re: Expression-based Contrast Ratio Calculatoron Jun 29, 2020 at 12:13:34 pm
```function sRGBtoLinear(rgba) { for (i=0; i &lt; 3; i++) { u=rgba[i]; if (u &lt;= 0.03928) { u = u/12.92; } else { u = (u + 0.055)/1.055; u = Math.pow(u,2.4); } rgba[i]=u; } return rgba; } function rgb_to_y(rgba) { return 0.2126*rgba + 0.7152*rgba + 0.0722*rgba; } txt1 = thisComp.layer("Color1NoTag").text.sourceText.value; txt2 = thisComp.layer("Color2NoTag").text.sourceText.value; rgb1 = hexToRgb(txt1); rgb2 = hexToRgb(txt2); l1=sRGBtoLinear(rgb1); l2=sRGBtoLinear(rgb2); y1=rgb_to_y(l1); y2=rgb_to_y(l2); ((Math.max(y1,y2)+0.05)/(Math.min(y1,y2)+0.05)).toFixed(2) +":1"```