Previous Up Next

19.1.3  Colors and color functions

Xcas uses RGB565 color encoding from FLTK and is therefore able to use at most 65536 RGB colors in graphs. In RGB565 encoding, there are only 32 values for the red and blue channels and 64 values for the green channel. The alpha channel (transparency) is not used.

Colors in Xcas are nonnegative integers with a dedicated subtype. They are evaluated to small colored squares in command line entries.

You can use either one of the predefined colors (see Section 19.1.2) or create color(s) by using the rgb, hsv and colormap commands (see below).

For example, to show all predefined colors, enter:

white,grey,olive,teal,cyan,green,brown,red,orange, gold,yellow,pink,magenta,purple,violet,blue,navy,black

The above colors are portable since they are defined in the FLTK colormap.

RGB and HSV colors

The rgb command converts between color objects and their RGB (Red, Green, Blue) specifications.

The hsv command converts between color objects and their HSV (Hue, Saturation, Value) specifications.

You can convert from RGB to HSV and vice versa by using the rgb2hsv and hsv2rgb commands.

Examples

rgb("#cf3f00"); rgb(0x00f3fc)
b:=rgb(216); rgb(255,0,0); rgb([0.0,1.0,0.0]);
spec:=rgb(b)
     

0,0,255
          
rgb2hsv(spec)
     

240,100,100
          
seq(hsv(15*h,100,80),h=0..20)

Colormaps

The colormap command returns built-in continuous/discrete colormaps or individual colors from the corresponding palettes. Note that the colors obtained this way are not guaranteed to be portable.

Examples

To visualize some colormaps, enter:

colormap(["inferno","viridis","phase","cubic","jet"],display)

The above colormaps do not appear perfectly seamless; this is due to the 16-bit color coding in Xcas instead of 24-bit RGB888 (which would allow for millions of colors). Note that clamping colors to 16 bits, which results in a slight posterization effect, pertains only to Xcas graphic commands, while images are displayed with exact colors (see Section 28.1.4).

You can invert and/or reverse colors in a colormap, obtaining a new one. A color is inverted by subtracting its R, G and B values from 255. For example, to obtain a “frozen” version of the inferno colormap, enter:

colormap("inferno",inv,reverse,display)

To get a single color from a colormap, enter e.g.

colormap("viridis",0.8)

To get a list of uniformly spaced colors, enter e.g.

colormap("blues",reverse,20)

To display the discrete colormap, enter:

axes=0; gl_ortho=1; seq(rectangle(k,k+1,2,color=filled+colormap("discrete",k)),k=0..35)

Useful color combinations can be generated from the discrete colormap by filtering out groups of similar colors. For example:

colormap("discrete","vivid")
colormap("discrete","strong")

You could also try passing e.g. "very light", "brilliant", "reddish", "green", "deep" etc. as the second argument (see Table 19.1). These and similar color combinations can be useful for categorical data visualization techniques such as bar plots, pie charts, clustering etc.

Color interpolation and RGB to XYZ conversion

The interp command interpolates between given colors in the CIE 1931 XYZ color space with the sRGB gamut (see here for details). In XYZ space, the Euclidean distance between two colors is roughly proportional to their perceived difference. Such interpolation is called perceptually uniform.

See Section 17.1.1, Section 17.1.2 and Section 28.1.10 for other uses of interp.

You can access the routines for RGBXYZ conversion by using the rgb2xyz and xyz2rgb commands which take a list or sequence of three real numbers in [0,1] representing the RGB or XYZ triple and return the other color specification in the same form. The rgb2xyz command can also take color objects as inputs. These routines apply chromatic adaptation with Bradford’s method to make the white color in Xcas the reference white (the default sRGB reference white is D65); thus the white color corresponds to [1.0,1.0,1.0] and black color to [0.0,0.0,0.0] in both RGB and XYZ.

Examples

To demonstrate the conversion between RGB and XYZ, enter:

xyz:=rgb2xyz(0,255,255)

or:

xyz:=rgb2xyz(0.0,1.0,1.0)

or:

xyz:=rgb2xyz(cyan)
     

0.561504215261,0.777146370045,0.982679191019
          
xyz2rgb(xyz)
     

0,255,255
          

To demonstrate color interpolation, enter:

c:=[red,yellow,green,cyan]:; interp(c,20)

The above list consists of RGB565 colors. To visualize a perceptually seamless transition from red to cyan passing through yellows and greens, you should convert c to a RGB matrix, which can be done by using apply and rgb. Enter:

res:=interp(apply(rgb,c),1000):;

The output is a 1000× 3 RGB matrix from which you can create an image by entering:

r,g,b:=[col(res,0)$100],[col(res,1)$100],[col(res,2)$100]:; img:=image(3,r,g,b);
     
an image of size 1000×100 (RGB)           

To display the result with RGB888 colors, enter:

display(img)

Previous Up Next