If you are playing around with Sheet colors with Apps Script, you sometimes find yourself with font colors that don’t go well with the background colors you’ve chosen. However, we can use Yiq values to decide whether the luminance of the background color would be best with a light or a dark foreground font color. Here’s a small Apps Script library to figure it out for you.
How to use
We’ll go through the details in this post later, but to get straight to it, you can use it like this after including the bmYiq library.
(Library key: 1gUNJ_axo9Q2tiBkZ5QFfLz03O1SMLR11Z-aZJoMzwImGWwSCwgH0SSW8
)
Get contrasting font colors for all the sheets in a range
Here’s how to examine all the background colors in a given range, pick a light or dark font color as appropriate, and apply them.
Here are some examples of doing selected ranges. Use whichever your favorite method is for creating ranges.
Set font colors for a whole sheet
Contrasting font colors for a header
For a specific column
More methods
The library provides a few more methods you might find useful.
Yiq.getContrasts ({backgrounds [, light, dark]})
Given an array of arrays of background colors in hex format ‘#xxxxxx’ , return the same size of array of contrasting foreground colors. See below if you want to do it yourself rather than use Yiq.setFontContrasts.
Yiq.getContrast({color [, light, dark]})
Return a single contrasting color for a given color.
If you know there’s only 1 color, you can optimize setting colors by using the setFontColor (as opposed to setFontColors), because that’ll make a shorter call to the spreadsheet api.
Yiq.setFontContrasts() does this automatically. It’ll calculate the fontColors for the range’s background colors, and if they are all the same it’ll optimize the call rather than sending a large array of individual cell colors if it can get away with it.
Light and dark settings
By default, Yiq uses #212121 for dark, and #ffffff for light. If you want to change that, each of the methods above take a couple of extra arguments to allow you to change those if you want.
Basing the background color on the font color
All the examples so far are for the most likely use case, ie basing the font color on the cell background, but if you want to do the reverse (find contrasting background colors for given font colors), you can do this.
Example
Here’s an screenshot of a collection of contrasting font colors chosen for some random background colors.

Yiq with Fiddler
Many of you use my Fiddler library Fiddler – A functional approach to fiddling with sheet data to deal with spreadsheet manipulation, and you can use Yiq with that too.
Contrast font color fiddler header format
Set contrast for entire sheet with fiddler
Set a header and particular column contrasts with fiddler
YIQ tldr
Read all about the background behind the YIQ calculation here. In summary, an rgb color can be converted into a YIQ value like this.

Heres’s what these values mean (source)
Attribute | Description |
---|---|
Y | Luma, or brightness of the image. Values are in the range [0, 1], where 0 specifies black and 1 specifies white. Colors increase in brightness as Y increases. |
I | In-phase, which is approximately the amount of blue or orange tones in the image. I in the range [-0.5959, 0.5959], where negative numbers indicate blue tones and positive numbers indicate orange tones. As the magnitude of I increases, the saturation of the color increases. |
Q | Quadrature, which is approximately the amount of green or purple tones in the image. Q in the range [-0.5229, 0.5229], where negative numbers indicate green tones and positive numbers indicate purple tones. As the magnitude of Q increases, the saturation of the color increases. |
Library code extract
For our purposes, we’re only interested in the Y of Yiq – that’s the luma of the color.
The code for this is in the bmYiq library and looks like this. You’ll find a link to the full code for the library at the end of this article.
Links
bmYiq library
Library key: 1gUNJ_axo9Q2tiBkZ5QFfLz03O1SMLR11Z-aZJoMzwImGWwSCwgH0SSW8
github: https://github.com/brucemcpherson/bmYiq
bmFiddler library
Library key: 13EWG4-lPrEf34itxQhAQ7b9JEbmCBfO8uE4Mhr99CHi3Pw65oxXtq-rU
github: https://github.com/brucemcpherson/bmFiddler
All the tests referenced in this article
github: https://github.com/brucemcpherson/testBmYiq
Related
Fiddler now supports joins to merge matching columns from multiple sheets
Read More
A fourth way to preserve and create formulas with Fiddler for Sheets, plus some more new methods
Read More
A third way to preserve formulas with fiddler, plus 2 new methods
Read More
2 ways to create and preserve formulas with fiddler for Google Apps Script
Read More
Handly helper for fiddler
Read More
Optimize updates to Google Sheets with fiddler fingerprints
Read More
Fiddler and rangeLists
Read More
Formatting sheet column data with fiddler
Read More
Header formatting with fiddler
Read More
Populating sheets with API data using a Fiddler
Read More
A functional approach to updating master sheet with Fiddler
Read More
Unique values with data fiddler
Read More
Fiddler – A functional approach to fiddling with sheet data
Read More
Content oriented color mixing with Apps Script
Read More
Obfuscate text with Apps Script
Read More
Markup HTML from JSON with Apps Script and JavaScript
Read More