Colors and Styles

The Wolfram Language doesn’t just handle things like numbers. It also for example handles things like colors. It lets you refer to common colors by their names.

Red

{Red,Green,Blue,Purple,Orange,Black}

You can do operations on colors. ColorNegate “negates” a color, giving the complementary color. Blend blends a list of colors together.

ColorNegate[Yellow]

Blend[{Yellow,Pink,Green}]

You can specify a color by saying how much red, green and blue it contains. The function RGBColor lets you do that, giving the amount of each color, from 0 to 1.

RGBColor[1,0,0]

RGBColor[1,1,0]

Table[RGBColor[1,g,0],{g,0,1,0.05}]

It’s often convenient to specify colors not directly in terms of red, green and blue, but for example instead in terms of hue. The function Hue lets you do this.

Hue[0.5]

Table[Hue[x],{x,0,1,0.05}]

Sometimes you may want to just pick a random color. RandomColor lets you do this. When you say RandomInteger[10], you’re asking to generate a random integer up to 10. But for a random color you don’t have to specify a range, so you can just write RandomColor[ ]—not giving any explicit input to the function.

RandomColor[ ]

Table[RandomColor[ ],30]

Blend[Table[RandomColor[ ],20]]

You can use colors in all sorts of places. For example, you can style output with colors.

Style[1000,Red]

Table[Style[RandomInteger[1000],RandomColor[ ]],30]

Another form of styling is size. You can specify a font size in Style.

Style[x,30]

Table[Style[100,n],{n,30}]

Table[Style[x,RandomColor[ ],RandomInteger[30]],25]

Make a list of red, yellow and green. »

Make a red, yellow, green column (“traffic light”). »

Compute the negation of the color orange. »

Make a list of colors with hues varying from 0 to 1 in steps of 0.02. »

Make a list of colors with maximum red and blue, but with green varying from 0 to 1 in steps of 0.05. »

Blend the colors pink and yellow. »

Make a list of colors obtained by blending yellow with hues from 0 to 1 in steps of 0.05. »

Make a list of numbers from 0 to 1 in steps of 0.1, each with a hue equal to its value. »

Make a purple color swatch of size 100. »

Make a list of red swatches with sizes from 10 to 100 in steps of 10. »

Display the number 999 in red at size 100. »

Make a list of the first 10 squares, in which each value is styled at its size. »

Use Part and RandomInteger to make a length-100 list in which each element is randomly Red, Yellow or Green. »

Use Part to make a list of the first 50 digits of 2^1000, in which each digit has a size equal to 3 times its value. »

Create a column of colors with hues varying from 0 to 1 in steps of 0.05. »

Make a list of colors varying from red to green with green components x varying from 0 to 1 in steps of 0.05 and with red components 1-x.  »

Create a list of colors with no red or blue, and with green varying from 0 to 1 and back down to 0 in increments of 0.1 (the 1 should not be repeated). »

Blend the color red and its negation. »

Blend a list of colors with hues from 0 to 1 in increments of 0.1. »

Blend the color red with white, then blend it again with white.  »

Make a list of 100 random colors. »

Make a column for each number 1 through 10, with the number rendered first in red then in green. »

Make columns of the numbers 1 through 10, rendered as plain, bold and italic in each column. »

What named colors does the Wolfram Language have?

Red, Green, Blue, Black, White, Gray, Yellow, Brown, Orange, Pink, Purple, LightRed, etc. In Section 16 we’ll see how to use (Key1[ctrl, =]) to enter any color name in plain English.

Why can colors be specified by red, green, blue values?

Basically because that’s how we humans perceive colors: there are three kinds of cells in our eyes that are roughly sensitive respectively to red, green and blue components of light. (Some other organisms work differently.)

What does color negation do?

It generates complementary colors, defined by computing 1−value (one minus the value) for each RGB component. If you negate the “display (emitted light) primaries” red, green, blue, you get the “print (reflected light) primaries” cyan, magenta, yellow.

What is hue?

It’s a way of specifying what are often called pure colors, independent of their tint, shade, saturation or brightness. Colors of different hues are often arranged around a color wheel. The RGB values for a particular hue are determined by a mathematical formula.

Are there other ways to specify colors than RGB?

Yes. A common one (implemented by Hue) is to use the combination of hue, saturation and brightness. LABColor and XYZColor are other examples. GrayLevel represents shades of gray, with GrayLevel[0] being black and GrayLevel[1] being white.

Tech Notes