Coding Color 01

coding color

I’m embarking on a new series of posts about coding color. And I’m pretty excited about this one.

Color is Complicated

… in which I attempt to overwhelm the reader by listing out everything you could possibly write about color. Feel free to skip.

Color is a complex subject to write about, because there are so many ways to approach it. What even is color?

We generally experience color as a property of things. “That apple is red.” It’s more accurate to say that color is a property of light, but we rarely say “the light bouncing off that apple is red.”

Color has to do with the wavelength of light. Photographers will also often talk about color temperature of their lights. All of that is very technical but doesn’t usually bleed into our every day experience of color. We never say “I like the wavelength of your dress”, though we do sometimes refer to warm and cool colors.

Scientifically, color is a spectrum of wavelengths. Or frequencies if you will. The longer wavelengths (lower frequencies) we see as reds moving into infrared, and the shorter wavelengths (higher frequencies) we see as violet moving into ultraviolet. But when we get down to the level of color theory in the arts, we somehow manage to wrap that spectrum around on itself and create a color wheel. Then we assign some of those wavelengths as primary, secondary and tertiary colors. But which wavelengths are primary, etc. depends on whether you’re talking about additive color or subtractive color.

Then we drop the term “wavelength” and start using “hue”. But hue is only one aspect of a color. There’s also value, saturation, luminosity, and value. We get tints and shades and tones.

Things get even more complex when we start letting colors interact. We get various color schemes: complementary, triadic, analogous, etc. And colors can have transparency, or alpha, so that when you overlay them, they blend. We see that one color looks totally different when it’s adjacent to another color. Some colors go well together, others “clash”.

And before long, we’re into psychology – how we perceive and experience colors, and what those colors mean and how they affect us. People with red cars supposedly drive fast and recklessly. Pink walls in prisons, hospitals, and schools make the “inmates” more calm, apparently. And you can’t go too far down that path without getting into how different cultures perceive and experience different colors.

On a very practical level, we want to be able to define and reproduce the same color on a consistent basis. We have hundreds, maybe thousands of names for colors, but what is “cornflower blue” exactly. Is my concept of cornflower blue the same as yours? So we’ve come to define colors by numbers. And we’ve come up with a bunch of different numbering systems that we call color models. Pantone, RGB, HSV, HSL, CMYK, CIE, LAB, to name some of the most common. Some of these are tailored to screen work and others more for print.

Even within one of the more common screen color models, RGB, there are many ways to specify a color: three percentages (0% – 100%), three floating point values (0.0 – 0.1), three decimal integer values (0 – 255), three hexadecimal values (0x00 – 0xFF), one large hexadecimal value (0x000000 – 0xFFFFFF), or one large decimal value (0 – 16777215). And we can throw an alpha channel in there and really mix it up.

Whew! All that is many, many books worth of material. And many, many books have already been written on these topics and are still cranked out on a regular basis.

The Plan

So what’s the goal for this series?

Well, the theme of these articles will be CODING color. So there’s a lot of the deep scientific and psychological stuff we can leave behind. And it will mainly be about digital screen work, not printing, so that narrows it down some. I’m not going to go too deep on color theory, but we’ll get into creating different types of color schemes.

In short, it’s going to be very practical, very much about writing functions to do different things with color in code. A lot about defining colors, altering them, converting between different color models, .

I think, because we’re talking about colors in regard to digital images on a screen, it’s fair to start out with a focus on RGB (and RGBA of course). We’ll talk about the RGB color channels and the different ways to represent the values of those channels, how to combine channels into a single color and extract the different channels from a color, how to alter a given color, and getting various information about a defined color.

We’ll look at some of the other color models – HSV, HSL, and CMYK at least. And learn how to convert from RGB to all of these models and vice versa.

There will be a little bit of color theory thrown in, but only on a very practical level. How to create various color schemes, creating color palettes, comparing two colors, etc.

And there’ll be a few miscellaneous and fun surprises along the way!

Table of Contents

I’ll fill this in as chapters are written.

The Format

As for code/pseudocode, we’ll follow the same general pseudocode format we used for The Coding Curves series. That’s all pretty well described in the intro post here: https://www.bit-101.com/blog/2022/11/coding-curves/.

There will be some added complexity for this series due to the different ways that colors are defined in different graphics APIs and the different methods that they make available. We’ll have to come up with a base set of methods we can assume to exist. So we’ll probably have to look at a few actual APIs and create some of those assumed methods for those that don’t have them. Fun.

I’m looking forward to writing these posts and I hope that people enjoy them and learn a few things.

One thought on “Coding Color 01

Leave a Reply