## getting the computer color wheel via pie charts

I found the following example in the Mathematica® (v 7) help file. I thought it had promise.

Hue[h, s, 1]], {s, 0, 1, .1}, {h, 0, 1, .1}

we see that they have set value (brightness) = 1, and varied the hue h and saturation s. (By varying s, they are displaying tints. We saw that here.) Still, this picture looks a little odd.

Let’s set a background, and hold the saturation s = 1. This will make a simpler color wheel out of it.

Ah, that’s clearer. And we see what’s odd – no pun intended – it has 11 sectors, and two of them look the same.

Of course: {h, 0, 1, .1} takes 11 values, from 0 to 1 inclusive in steps of 1/10… and we know that h = 1 is the same as h = 0. These are easy to fix: do not let h go all the way to 1, and use steps of 1/12 instead of 1/10.

OK… but let’s make one last change: shift the sector origin by an additional 15° (= Pi/12), so the red slice is centered. (The original shift of Pi/2 is what put red near the top instead of at the right.) We get

One advantage of letting Mathematica do all this is that it’s awfully easy to get a finer drawing: for example, change the increment on h to 1/24, and change the additional offset to Pi/24. (My originals were done laboriously in Illustrator, one sector at a time.)

## getting the artists’ color wheel

It would sure be nice if I could get Mathematica to draw an artists’ color wheel. Let me recall the one I had in this post:

It used the CMYK specs from my “Color Harmony Workbook”. I want to try something different: can I use the HSV colors, and just put them in different places? It’s a little awkward having the finer drawing, with 24 sectors, so close, so let me recall the computer color wheel with 12 sectors.

What’s the simplest thing I could do?

Leave red and blue alone. (Yes, that changes some of the colors in the region from blue to red – but it puts the computer’s blue right where it belongs on the artists’ color wheel.) Move yellow from 60° to 120°. Move green from 120° to 180°. Linearly interpolate between the new locations.

That is, if I am given a hue between 0 and 120°, halve it… if I’m given a hue from 120° to 180°, just subtract 60°… if I’m given a hue between 180° and 240°, double it and subtract 120°. Any angle from 240° to 360° is left unchanged.

(This may seem backward, but it isn’t.)

Here’s a piecewise function that does that:

(I have no idea where that fifth line came from, but things still seem to be working correctly.)

Here are the points under discussion and here’s a picture:

Now we just need to call it. Instead of Hue[h, 1, ,1]… I want to go from h to 360 h (angles in degrees)… call toArt… then divide by 360 so the first argument to Hue is in the range [0, 1].

Note that I am going to name this copy g1. We’ll be using it with other drawings.

So. I have an artists’ color wheel, but this one uses the colors on my computer – which is what I really want.

Let me take a closer look at why it works but may seem backwards. We start with a list of equally-spaced angles:

At the angle 120°, for example, the computer color wheel has green; I want yellow, instead, which is the hue at 60°. And that’s why I cut the given angle in half, to assign the hue of 60° to the angle of 120°. The second column shows the new angles, computed using my toArt function.

## looking at a color scheme (palette)

That is, let’s start playing.

Here are four colors from Cabarga’s “Global Color Combinations”, p. 30 (see the bibliographies page), henceforth “Cabarga” for the rest of this post.

I ask Mathematica to draw a pie chart with each of them the same size – that’s the {1, 1, 1, 1} :

Those colors look right – they look like what I see in the book.

I could have chosen to emphasize one of them:

I could have used fancier partitions. Here’s {1,2,3,4}:

Here’s a more extreme partition, {1, 8, 27, 64}, which reduces the orange to a highlight role.

So far, so good. I can play with a given palette of colors. I could also use RotateRight or RotateLeft to shift the list of colors, thereby emphasizing a different color.

## locating the colors on the artists’ color wheel

This gets a little complicated, and I’ll show you why later in this post. The problem is this. The good news is that Mathematica drew those colors just fine, even though they were given in CMYK. The bad news is that Mathematica can’t tell me what he drew.

Why do I care? Because I want HSV (= HSB) coordinates for those colors. Both Mathematica and I can translate RGB to HSV correctly – but Mathematica cannot translate CMYK to either RGB or HSV right. I’ll demonstrate that later.

It’s frustrating. Mathematica drew the colors fine… but if I ask him for RGB or HSV coordinates, he gets wrong, useless answers. Fortunately, I have other tools.

For now, I open my DigitalColor Meter and I ask it what the RGB coordinates of those four colors are. I get answers as percentages and I divide by 100:

Next, I open my ColorSync Utility, and I ask it to go from generic CMYK to RGB on my monitor. I get

They differ by no more than .001:

Great. Mathematica seems to have used my monitor profile to draw the given CMYK colors as RGB on my monitor. What it drew matches the ColorSync calculations, and matches the book nicely.

Having RGB values, I can convert to HSV… then convert to the artists’ color wheel… and then place these four colors on the artists’ color wheel.

Well, there’s just one little problem. We need the inverse function for “toArt”.

Recall toArt:

Then the inverse function should be

(I know. That’s the function that we might have expected for getting the artists’ color wheel in the first place.)

We can draw them both:

Here’s my module for locating a list of colors on the artists’ color wheel. It takes one input list, which must be RGB in [0,1]. It converts to HSV (using my code “toHSV”, but it could have used Mathematica’s, once I supply the RGB)… then changes the angles… and locates the points. There are two oddities: our 0° is at the top and angles go CW – and that’s why the x- and y- coordinates of the point appear to be switched (Sin for x, Cos for y).

Oh, it prints the modified angles. They comfort me, because shades and tones often don’t look like the colors they get assigned to.

So, call it:

(Boy that looks easy. I wish I could say that I typed the code in and got that result, but no… it took a while.)

Now, overlay it on the artists’ color wheel that we named g1:

It’s only a bit of a stretch to call the three colors on the right an analogous set… and the blue is complementary… and I got Mathematica to do almost all the work for me.

I’ll admit that the tones ad shades don’t look as though they really belong where they are… but I’m pretty sure they do. That’s one reason for printing the angles. In addition, I can – and did – redraw the color wheel with saturation and intensity set to match that purplish-blue – and I could see that the big dot nearly matches the resulting blue tone. (I didn’t change the hue angle, so we’ve got 232° for the big dot and 240° for the sector.)

I didn’t take the time to do the same for the other three colors.

## another example

Here’s another example, the one shown in this diary post. It’s from p. 25 of Cabarga. It has 7 colors, but I chose to do them in groups of no more than four. Here are the first four CMYK specs:

I also didn’t want to type lists of 1s, so I take what I need from a constant array:

Here’s the other three CMYK specs:

And here is a pie chart of all seven colors:

Now I measure the RGB values, and paste them into a list, “meas”. (I didn’t put braces around them as I entered them, so I did it afterwards with the Partition command.)

We can combine the overlay command directly with the disks command, without creating an intermediate drawing:

We clearly have two sets of analogous colors (bottom and right) and we have a complementary color for one set. Again, this is something I want to know of a color combination that I like.

I like this new tool of mine.

## what Mathematica does wrong

Now let’s do a little mathematical detective work. Recall the first, smaller example.

Ask Mathematica to convert them to RGB. I need to write it as a function and apply it to each one.

I’m not going to draw them, but those are a far cry from the measured values:

It gets worse. Ask Mathematica to convert its own RGB back to CMYK:

One of those, the first, agrees with the original spec. The other three do not match the originals.

Let me summarize the relevant lists of specifications:

- a – the given CMYK
- meas – the measured RGB from the pie chart
- rgb – Mathematica’s conversion of “meas” from CMYK to RGB
- cmy – Mathematica’s conversion of “rgb” from RGB to CMYK.

Only one of the four pairs of specs of “meas” and “rgb” is the same; only the one corresponding pair of “a” and “cmy” is the same. The drawings are right, but Mathematica can’t correctly describe what he drew.

Could I figure it out? Yes.

It took me a while to decide exactly what Mathematica appears to be doing, but the starting point is a simple idea I’ve seen before: translate between RGB and CMY (not CMYK, but CMY) by taking the differences from (1,1,1).

The first spec

has no black. Subtract it from (1,1,1}. Well, let Mathematica do the work. Start with the CMYK… make a list of it… drop the last component… and subtract… and change the head to RGBColor:

And that’s what Mathematica got when asked to convert the first CMYK spec to RGB:

And we saw that he goes backwards just fine – if there’s no black.

OK… what if we have black? For this one, the relevant idea is another simple one – but going in the other direction. Given an RGB spec, compute the differences from (1,1,1) as before, but now set black to the minimum of those values. Why? After we subtract from (1,1,1), we’re looking at CMY; but an equal mixture of C+M+Y is black, K, in theory, so subtract that common value out.

Consider Mathematica’s second calculation… start with the RGB… change it to a list… subtract from (1,1,1) to get CMY… set K = the minimum… subtract K from the other three values… append it… and change the head to CMYK…

And that’s what Mathematica got when it converted its own second RGB back to CMYK:

We can confirm the rest of Mathematica’s translations from his RGB to CMYK. Here’s the third spec:

And that’s what he had gotten directly:

Here’s the fourth spec:

Right, again.

Now we can tackle the rest of Mathematica’s translation from CMYK, when K is nonzero. We know that he wants to interpret K has a common value of CMY, so we need to add it to whatever they are.

Here’s the second spec… make a list… call the last component K… drop the last component…add the K value to each of CMY…

Oops. We have Y > 1. No problem, cut it off at 1; cut any of them off at 1. Start where we left off… Truncate the 1.1 to 1… now subtract these CMY values from (1,1,1).. and change the head to RGBColor…

Is that what Mathematica got? Yes:

Here’s the third CMYK spec:

Is that what Mathematica got? Yes:

Here’s the fourth CMYK spec:

Yup…

I think I know how Mathematica is converting between RGB and CMYK. Too bad he isn’t using my monitor profile.

OK. That’s not exhaustive, but I believe it. Besides, what matters is that I can’t use Mathematica to convert CMYK to RGB – and it turns out, I can’t convert to HSB either, but I haven’t figured out what he does for that. It isn’t just a translation from CMYK to RGB to HSB.

So. Mathematica can be used to display CMYK specs taken from a book – but I have to use the DigitalColor Meter to determine the RGB values on my screen. And from them I can get HSV.

(Just don’t try to use one of the chart colors for the background; Mathematica will change it. I had hoped it would be an easy way to specify the dominant color in a palette. No can do, not reliably anyway.)

I don’t know about you, but I enjoyed all this.

June 16, 2011 at 10:28 am

Excellent work Rip!

It’s painful that even in Mathematica 8 there’s no link to the CMM. No way to do accurate color conversions through PCS.

Enjoying your blog,

Bob

June 16, 2011 at 3:47 pm

Thanks, Bob.

I’m really, really glad I have the DigitalColor Meter and the ColorSync utility.