Reverse Engineering the Blending Mode of Gilbert Color
Tags: css, learning
Published: 2019-04-06 19:47:32

I’ve been preparing for my [Talk.CSS](http://singaporecss.github.io) talk about `mix-blend-mode` this week. Meanwhile, if you happen to have come across this cute little community called SingaporeCSS, and are even in our facebook group, you’d notice that the banner, though dates back to Jan 2018, uses an interesting display font.

![](https://paper-attachments.dropbox.com/s_3FE563BBEB57460B63252FD6A500CCA308DF4EC01CA05F49A0596CC2D75D6AD8_1554523322197_image.png)

The font was made to commemorate [Gilbert Baker](<https://en.wikipedia.org/wiki/Gilbert_Baker_(artist)>), who designed the rainbow flag.

![PRkit.jpg](https://static1.squarespace.com/static/58ed2d6244024330c0a58707/t/5940481b725e252076c4cddf/1497384996193/?format=2500w)

It is a [color font](https://www.w3.org/TR/css-fonts-4/#color-font-support) and you may download both the color version and the normal version from the [Type With Pride website](https://www.typewithpride.com/).

![TypeWithPride-11.jpg](https://static1.squarespace.com/static/58ed2d6244024330c0a58707/58ed2dbbf7e0abfc5d7ebb1c/58efdb79bebafbc81fb63c3e/1492114301044/TypeWithPride-11.jpg?format=2500w)

Aside from being incredibly beautiful, you cannot miss the blending effect on the intersections of the strokes. And hey, remember the topic for my talk? This CSS property `mix-blend-mode` is exactly about how colors blend. And so now it becomes the font of my slides.

![B.jpg](https://static1.squarespace.com/static/58ed2d6244024330c0a58707/58ed9e0686e6c03e56a1e29d/58ee39a39de4bb85a0b9ec13/1492007334100/B.jpg?format=2500w)

## Figuring out the blending mode

One side effect from preparing for a talk on `mix-blend-mode` is that you cannot stop but wonder what’s the blending mode (or not any) used on each of the intersection of colors you see. And mid-way through slides preparation I felt this increasing temptation to figure out what’s on my chosen font. I mean, what if someone asks that question?

I’m not very well trained in looking at colors. And I cannot tell how the colors are blended just by naked eyes. I guess since I’m learning something about the blending modes, why not try my luck to figure it out?

Now, the reverse-engineering requires some understanding of [CSS Colors](http://www.w3.org/TR/css-color-4/) and [Blending](https://www.w3.org/TR/compositing-1/#blending), which I’ll summarize just enough for this process:

- 12 out of 16 of the blending modes are _Separable Blending Modes_ computed in the sRGB color space. _Separable_ means the R, G, and B channels of the resultant color are computed separately. The 4 others are _Non-Separable_ and are computed in the HSL representation.
- The computation uses the fractional values of each color, i.e., “0xFF” or 255 means 1, “0x80” or 128 means 0.5.

You may check [the specs](https://www.w3.org/TR/compositing-1/#blending) for the the complete list of CSS specified blending modes.

There is no guarantee that I can find my match in the separable blending modes, and maybe not even any CSS specified blending modes. The designers of the font may pick any colors at all for the intersections of the strokes. Who knows 🤷🏻‍♀️

Still, I dumped the typographer’s favorite quote on to a white background and started doing this:

![](https://paper-attachments.dropbox.com/s_3FE563BBEB57460B63252FD6A500CCA308DF4EC01CA05F49A0596CC2D75D6AD8_1554525398416_image.png)

And here are the colors

```css
--orange: rgb(255, 107, 0); /* #ff6b00 */
--red: rgb(255, 67, 56); /* #ff4338 */
--green: rgb(5, 206, 124); /* #05ce7c */
--blue: rgb(0, 179, 227); /* #00b3e3 */
--magenta: rgb(218, 58, 179); /* #da3ab3 */
--yellow: rgb(255, 200, 67); /* #ffc843 */
```

Basically, I’ve decided to try every blending function and see which one matches. And I’d like to first check if there are any easy eliminations.

Normal, for example, says there’s no blending at all.

Darken and lighten pick the darker or the lighter channel values, respectively. So the result should have numbers matching either one of the channels of the constituent colors, but there is none except for zeroes.

The first one that has a non-trivial blending computation is multiply, which is essentially multiplying the colors by channels. Elementary school math taught us that zero times any number gives you zero. So I tried my luck on a few combinations that involve two colors with zeros:

Reading from the color pickers:

- green x orange: `#055600`, or `rgb(5, 86, 0)`
- blue x red: `#002f32`, or `rgb(0, 47, 50)`
- blue x magenta: `#00299f`, or `rgb(0, 41, 159)`

Then I did the math, rounded to the nearest integer:

- green x orange

  R: (5 / 255) \* (255 / 255) = 5 / 255

  G: (206 / 255) \* (107 / 255) = 86 / 255

  B: (227 / 255) \* (0 / 255) = 0 / 255

And I get… `rgb(5, 86, 0)` which is exactly the intersection! I tried a few combinations, and they all matched. 😱　Did I just got hit by shit luck?

Now, as a Chinese term goes, 胆大心细 which means you should be bold but careful, is a virtue for mathematicians. I should verify that it is indeed multiply, and not anything else.

A few more can be eliminated by naked eyes, as well as easily verifiable with simple math. If you are keen, I have this color palette on CodePen as well: 

{% codepen https://codepen.io/wgao19/pen/zXrpEG %}

It is not screen, because screening any color with 0 gives you back the color.
It is not color-dodge, because B?(x, 1) does not yield 1.
It is not color-burn, because B?(1, x) does not yield 1.
… and I eventually ended up realizing that there aren’t that many intersections after all. All of them are multiply, for real. QED. Although, that’s not to guarantee anything for future reference... The designers may change their mind 🤷🏻‍♀️

**Multiply and darken**

While darken was easily eliminated by logic, I noticed how multiply and darken look very similar in this situation. The side by side comparison on that CodePen may give you an idea, and let me paste a screenshot again:

![Side by side comparison between multiply (top) and darken (bottom) on single colors.](https://paper-attachments.dropbox.com/s_3FE563BBEB57460B63252FD6A500CCA308DF4EC01CA05F49A0596CC2D75D6AD8_1554577534840_image.png)

With single colors, since both darken and multiply yield a weaker value in each color channel, they may very well produce very similar resultant colors. Where they differ drastically is when applied to photos, or any graphics with more complex contours and textures. Because multiply is a linear operation, it preserves the texture of both the graphics. But darken simply picks the smaller value on each channel and hence there is no guarantee the texture of any side can be preserved.

![Side by side comparison of multiply (left) and darken (right). Notice how some of the textures of the duck is lost due to the pixels getting replaced by the result of a non-linear operation.](https://paper-attachments.dropbox.com/s_3FE563BBEB57460B63252FD6A500CCA308DF4EC01CA05F49A0596CC2D75D6AD8_1554577656208_image.png)

**A detour**

There was a detour in this process. I was originally playing with Chrome and the numbers never matched with my computation. They were close, but the resultant values were always off by a few integers. And when playing with `mix-blend-mode: screen` they were often even off by two digits 😲

I nearly thought I did not understand mix blend mode at all. Turns out there’s a [bug](https://bugs.chromium.org/p/chromium/issues/detail?id=880059&q=mix-blend-mode&colspec=ID%20Pri%20M%20Stars%20ReleaseBlock%20Component%20Status%20Owner%20Summary%20OS%20Modified) in Chrome that may be related. Never be too sure of yourself, never be too sure the mistake is at your side neither 😬. Firefox, on the other hand, happily spat out my results with a few exceptions that were at most off by rounding on the unit’s digit. It looks like Firefox is rounding up, where I’m doing the traditional math rounding.

## A block quote border blended

I thought I’d do something to match my slides’ theme with the font’s design. Since I now have known the secret of their colors, why not use that knowledge for free man.

I wanted to create a blended border for my block quote elements, where I’ll use the colors to cover the border, make them intersect each other with the matching blending mode for the intersections.

I decided to go with multiple background images, and use `background-blend-mode: multiply`.

Initially I was thinking along the line of stacking an array of background linear gradient images with some overlap.

```css
blockquote {
  background-image: linear-gradient(
      to bottom,
      var(--magenta) 0,
      var(--magenta) 100%
    ), linear-gradient(to bottom, var(--blue) 0, var(--blue) 100%),
    linear-gradient(to bottom, var(--yellow) 0, var(--yellow) 100%),
    linear-gradient(to bottom, var(--orange) 0, var(--orange) 100%),
    linear-gradient(to bottom, var(--green) 0, var(--green) 100%),
    linear-gradient(to bottom, var(--red) 0, var(--red) 100%);
  background-size: 8px calc(100% / 4.5); /* to create the overlap */
  background-repeat: no-repeat;
  background-position-y: 0, calc(100% / 5), calc(200% / 5), calc(300% / 5), calc(
      400% / 5
    ), calc(500% / 5);
  background-blend-mode: multiply;
}
```

They worked. But they turned out to look rather uninteresting..

![](https://paper-attachments.dropbox.com/s_3FE563BBEB57460B63252FD6A500CCA308DF4EC01CA05F49A0596CC2D75D6AD8_1554575118585_image.png)

This is personal, but I added a tweak on the gradient directions that made me really happy. The final result looks like this:

![](https://paper-attachments.dropbox.com/s_3FE563BBEB57460B63252FD6A500CCA308DF4EC01CA05F49A0596CC2D75D6AD8_1554569507931_image.png)

You may check out the implementation in this CodePen. Note that the color part works only on [browsers that support color font](https://www.colorfonts.wtf/).

{% codepen https://codepen.io/wgao19/pen/qwNgMj %}

## Till next time 🤞

If you happen to be in Singapore this month, and are designer or front end dev interested in one another, maybe come to [Talk.CSS #38](https://singaporecss.github.io/38/) to hear me talk about this CSS property called mix blend mode. Or if you are inspired by this little work and have created something interesting, please [show and tell](https://airtable.com/shrahPiYG3oCtOM6e?fbclid=IwAR0mw6xqqkXJVoi-YS7yQ8ITEWyfBcasHoyWSXznScKQWjM1IUYU3cl4vo0) us 👀

Fingers crossed for CodePen to be back in shape though.

## References

- [CSS Compositing and Blending Module Level 1](https://www.w3.org/TR/compositing-1)
- [CSS Font Module Level 4](https://www.w3.org/TR/css-fonts-4)
- [Talk.CSS](http://singaporecss.github.io)
- [Type With Pride](https://www.typewithpride.com/)
- [Color Font](https://www.colorfonts.wtf/)