How to represent functions using colors

Basics of colors

Mathematica has many ways of communicating colors. The simplest one is Hue[]. The variable in Hue is between 0 and 1

Graphics[{Hue[0.8],Disk[{0,0},1]},PlotRange->{{-1.5,1.5},{-1.5,1.5}}]

Below is a red, that is RGBColor[1,0,0},

Graphics[{RGBColor[1,0,0],Disk[{0,0},1]},PlotRange->{{-1.5,1.5},{-1.5,1.5}}]

Below is a dark red, maroon, that is RGBColor[1/2,0,0}. The vector {1/2,0,0} is the position vector of point which is the midpoint between the red color (1,0,0} and the black color {0,0,0}.

Graphics[{RGBColor[1/2,0,0],Disk[{0,0},1]},PlotRange->{{-1.5,1.5},{-1.5,1.5}}]

Below is a light red, that is RGBColor[(1-1/2){1,0,0}+(1/2){1,1,1}}. The vector (1-1/2){1,0,0}+(1/2){1,1,1} is the position vector of point which is the midpoint between the red color (1,0,0} and the white color {1,1,1}.

t=1/2;Graphics[{RGBColor[(1-t){1,0,0}+t{1,1,1}],Disk[{0,0},1]},PlotRange->{{-1.5,1.5},{-1.5,1.5}}]

Here we explore the light shades of red. All the colors from red, that is the vector {1,0,0} to the white color, the vector {1,1,1}:

Manipulate[Graphics[{RGBColor[(1-t){1,0,0}+t{1,1,1}],Disk[{0,0},1]},PlotRange->{{-1.5,1.5},{-1.5,1.5}}],{t,0,1}]

Here we explore the dark shades of red. All the colors from red, that is the vector {1,0,0} to the black color, the vector {0,0,0}:

Manipulate[Graphics[{RGBColor[(1-t){1,0,0}],Disk[{0,0},1]},PlotRange->{{-1.5,1.5},{-1.5,1.5}}],{t,0,1}]

Next we explore the transition from Black to White, through all possible shades of Red:

Manipulate[Graphics[{RGBColor[If[t<1,t {1,0,0},(1-(t-1)){1,0,0}+(t-1){1,1,1}]],Disk[{0,0},1]},PlotRange->{{-1.5,1.5},{-1.5,1.5}}],{t,0,2}]

Many colors in one picture

Many colors with disks

Coloring 10 disks with centers at points {0,0}, {1,0}, {2,0} and so on, up to {10,0}, and with the radius 1, in different colors using Hue[]. Recall that the variable in Hue[] is between 0 and 1

Graphics[Table[{Hue[(k/10)],Disk[{k,0},1]},{k,0,10,1}],PlotRange->{{-1.5,11.5},{-1.5,1.5}}]

Coloring 100 disks in different colors using Hue[]

Graphics[Table[{Hue[(k/100)],Disk[{k,0},1]},{k,0,100}],PlotRange->{{-1.5,101.5},{-1.5,1.5}},ImageSize->400,AspectRatio->0.1]

Instead of Hue[] we could have used the shades of dark red:

Graphics[Table[{RGBColor[(k/10){1,0,0}],Disk[{k,0},1]},{k,0,10,1}],PlotRange->{{-1.5,11.5},{-1.5,1.5}}]

Graphics[Table[{RGBColor[(k/100){1,0,0}],Disk[{k,0},1]},{k,0,100}],PlotRange->{{-1.5,101.5},{-1.5,1.5}},ImageSize->400,AspectRatio->0.1]

To do the same with the light shades of red is as follows:

Graphics[Table[{RGBColor[(1-(k/10)){1,0,0}+((k/10)){1,1,1}],Disk[{k,0},1]},{k,0,10,1}],PlotRange->{{-1.5,11.5},{-1.5,1.5}}]

Graphics[Table[{RGBColor[(1-(k/100)){1,0,0}+((k/100)){1,1,1}],Disk[{k,0},1]},{k,0,100}],PlotRange->{{-1.5,101.5},{-1.5,1.5}},ImageSize->400,AspectRatio->0.1]

Many colors with rectangles

A convenient way of generating lists of numbers in Mathematica is offered by the function Range[]

Range[0,1,1/10]

Range[0,1,1/100]

A convenient way of applying a function to a list is given by so called pure function. To make the Sin function into a pure function we write

Sin[#]&

But, more importantly, here is how to make the square function into a pure function

(((#)^2))&[2]

Apply the square to a list

(((#)^2))&/@Range[0,1,1/10]

Demonstrating Hue[] on the unit interval using many rectangles, created using the function Polygon[]. To get a rectangle, I give a list of five points to Polygon[]. For example, Polygon[{{0,0},{2,0},{2,1},{0,1},{0,0}}] is the polygon with vertices {0,0},{2,0},{2,1},{0,1}. Below, st is the step, that is the width of each small rectangle, cc is just a correction number, and hh is the height of each rectangle, LL is the length of the colored area.

st=(1/200);cc=0.002;hh=0.1;LL=1;Graphics[{
{Hue[#],Polygon[{{#-(st/2)-cc,0},{#+(st/2)+cc,0},{#+(st/2)+cc,hh},{#-(st/2)-cc,hh},{#-(st/2)-cc,0}}]}&/@Range[0+(st/2),LL-(st/2),st],{Thickness[0.005],Line[{{0-(st/2),0},{LL+(st/2),0},{LL+(st/2),0.1},{0-(st/2),0.1},{0-(st/2),0}}]}},Frame->True,FrameTicks->{{None,None},{Automatic,None}},ImageSize->400]

Make the step st smaller

st=(1/10);cc=0.000;hh=0.1;LL=1;Graphics[{
{Hue[#],Polygon[{{#-(st/2)-cc,0},{#+(st/2)+cc,0},{#+(st/2)+cc,hh},{#-(st/2)-cc,hh},{#-(st/2)-cc,0}}]}&/@Range[0+(st/2),LL-(st/2),st],{Thickness[0.005],Line[{{0-(st/2),0},{LL+(st/2),0},{LL+(st/2),0.1},{0-(st/2),0.1},{0-(st/2),0}}]}},Frame->True,FrameTicks->{{None,None},{Automatic,None}},ImageSize->400]

Next, I want to produce a coloring of the unit interval which will transition from white to red.

First, this is the Red Color

RGBColor[1,0,0]

This is the White Color

RGBColor[1,1,1]

With Hue[] we just use Mathematica preprogrammed colors. Next we want to control the colors. We do that by transitioning  How you transition from red to white is by using convex combination of vectors

(t)*{1,0,0}+(1-t) {1,1,1}

At t = 0 we are at white

((t)*{1,0,0}+(1-t) {1,1,1})/.{t->0}

At t=1 we are red

((t)*{1,0,0}+(1-t) {1,1,1})/.{t->1}

The formula

(t)*{1,0,0}+(1-t) {1,1,1}

represented as a pure function is

((#)*{1,0,0}+(1-#) {1,1,1})&

Or simplified

Simplify[#1 {1,0,0}+(1-#1) {1,1,1}]

We do not need to write #1, since we always have one variable only.

Below we use rectangles to show transition from 0 represented by white to red represented by 1 and all light shades of red in-between.

st=(1/200);cc=0.001;hh=0.1;LL=1;Graphics[{
{RGBColor[1,1-#,1-#],Polygon[{{#-(st/2)-cc,0},{#+(st/2)+cc,0},{#+(st/2)+cc,hh},{#-(st/2)-cc,hh},{#-(st/2)-cc,0}}]}&/@Range[0+(st/2),LL-(st/2),st],{Thickness[0.005],Line[{{0-(st/2),0},{LL+(st/2),0},{LL+(st/2),0.1},{0-(st/2),0.1},{0-(st/2),0}}]}},Frame->True,FrameTicks->{{None,None},{Automatic,None}},ImageSize->400]

Functions in color

Recall the last picture in which we represented how the numbers between 0 and 1 are represented by the light shades of red:

st=(1/200);cc=0.001;hh=0.1;LL=1;Graphics[{
{RGBColor[1,1-#,1-#],Polygon[{{#-(st/2)-cc,0},{#+(st/2)+cc,0},{#+(st/2)+cc,hh},{#-(st/2)-cc,hh},{#-(st/2)-cc,0}}]}&/@Range[0+(st/2),LL-(st/2),st],{Thickness[0.005],Line[{{0-(st/2),0},{LL+(st/2),0},{LL+(st/2),0.1},{0-(st/2),0.1},{0-(st/2),0}}]}},Frame->True,FrameTicks->{{None,None},{Automatic,None}},ImageSize->400]

We can use this encoding of numbers with the light shades of red to represent functions. Consider first the function Sin[x] defined on the interval [0,π]. The values of Sin[x] on this interval are between 0 and 1.

The color graph below represents the sin function where the values near 0 are nearly white while the values near 1 are nearly red.

st=(Pi/200);cc=0.002;hh=0.2;LL=Pi;Graphics[{
{RGBColor[1,1-Sin[#],1-Sin[#]],Polygon[{{#-(st/2)-cc,0},{#+(st/2)+cc,0},{#+(st/2)+cc,hh},{#-(st/2)-cc,hh},{#-(st/2)-cc,0}}]}&/@Range[0+(st/2),LL-(st/2),st],{Thickness[0.005],Line[{{0-(st/2),0},{LL+(st/2),0},{LL+(st/2),hh},{0-(st/2),hh},{0-(st/2),0}}]}},Frame->True,FrameTicks->{{None,None},{Range[0,Pi,(Pi/8)],None}},ImageSize->400]

Below is the square of the cosine function from 0 - white to 1 - red in the shades of red.

st=(Pi/200);cc=0.002;hh=0.2;LL=Pi;Graphics[{
{RGBColor[1,1-(((Cos[#]))^2),1-(((Cos[#]))^2)],Polygon[{{#-(st/2)-cc,0},{#+(st/2)+cc,0},{#+(st/2)+cc,hh},{#-(st/2)-cc,hh},{#-(st/2)-cc,0}}]}&/@Range[0+(st/2),LL-(st/2),st],{Thickness[0.005],Line[{{0-(st/2),0},{LL+(st/2),0},{LL+(st/2),hh},{0-(st/2),hh},{0-(st/2),0}}]}},Frame->True,FrameTicks->{{None,None},{Range[0,Pi,(Pi/8)],None}},ImageSize->400]

Exp[-Pi]

N[Exp[-Pi]]

Below is the graph of the function (e^(-x))=Exp[-x]  on the interval [0,π] represented from 0 - white to 1 - red in the shades of red.

st=(Pi/200);cc=0.002;hh=0.2;LL=Pi;Graphics[{
{RGBColor[1,1-Exp[-#],1-Exp[-#]],Polygon[{{#-(st/2)-cc,0},{#+(st/2)+cc,0},{#+(st/2)+cc,hh},{#-(st/2)-cc,hh},{#-(st/2)-cc,0}}]}&/@Range[0+(st/2),LL-(st/2),st],{Thickness[0.005],Line[{{0-(st/2),0},{LL+(st/2),0},{LL+(st/2),hh},{0-(st/2),hh},{0-(st/2),0}}]}},Frame->True,FrameTicks->{{None,None},{Range[0,Pi,(Pi/8)],None}},ImageSize->400]

These are the powers of Sin[x] function from 0 - white to 1 - red in the shades of red.

st=(Pi/200);cc=0.002;hh=0.2;LL=Pi;
Manipulate[Graphics[{
{RGBColor[1,1-(((Sin[#]))^nn),1-(((Sin[#]))^nn)],Polygon[{{#-(st/2)-cc,0},{#+(st/2)+cc,0},{#+(st/2)+cc,hh},{#-(st/2)-cc,hh},{#-(st/2)-cc,0}}]}&/@Range[0+(st/2),LL-(st/2),st],{Thickness[0.005],Line[{{0-(st/2),0},{LL+(st/2),0},{LL+(st/2),hh},{0-(st/2),hh},{0-(st/2),0}}]}},Frame->True,FrameTicks->{{None,None},{Range[0,Pi,(Pi/8)],None}},ImageSize->400],{nn,Range[1,20],Setter,ControlPlacement->Top}]

Manipulate[Plot[(((Sin[x]))^nn),{x,0,Pi},PlotRange->{0,1},AspectRatio->Automatic,ImageSize->400],{nn,Range[1,20],Setter},ControlPlacement->Top]