GNU TeXmacsUser manualReferenceAboutEnglish
Colors

1.The color word

A color is a plain unsigned int (

Kernel/Abstractions/basic.hpp

). In the normal true color mode (the global true_colors, which is true by default) it holds the four components in the order alpha, red, green, blue, from the most to the least significant byte:

color rgb_color (int r, int g, int b, int a= 255);
// = (a << 24) + (r << 16) + (g << 8) + b

Components range from 0 to 255; alpha 255 is opaque and 0 fully transparent. get_rgb_color decodes a color word; there is also a variant returning an array<int> of four components, and rgb_color (array<int>) in which missing components default to 255.

The other mode (set_true_colors (false)) is a relic of indexed displays: colors are then mapped to a palette of CFACTOR \(\times\) CFACTOR \(\times\) CFACTOR colors and GREYS grey levels (set_color_attrs, get_color_attrs), and the low 24 bits contain a palette index. Only the X11, SDL and Vue ports call initialize_colors, and only X11 could use the indexed mode (it tries set_true_colors (false) first); with Qt and Cocoa the basic colors are initialized statically and true colors are always used.

The file also defines global colors black, white, red, …, light_grey, grey, dark_grey and tm_background, which are used throughout the kernel for interface elements, and the value pastel (223) used for the pastel colors of the TeXmacs dictionary.

2.Named colors

Colors in documents are strings: the value of color, bg-color, fill-color and similar variables, the color arguments of with, of pencils and brushes and of graphical objects. They are converted by

color named_color (string s, int a= 255);

which accepts, after converting s to lower case,

Because of the order, a name defined in several dictionaries takes its TeXmacs value: orange is #ff8000, not the SVG #ffa500. The dictionaries are filled into hash tables on the first lookup. Unknown names yield black; to tell an unknown name from black, use is_color_name, which treats a result with zero RGB components as valid only for "black" and strings starting with "#000".

The second argument multiplies the alpha of the color: the typesetter passes the current opacity, edit_env_rep::alpha, which is decoded from opacity (

Typeset/Env/env_semantics.cpp

), so edit_env_rep::get_color (var) returns a color already combined with the opacity. Pencils and brushes (

Graphics/Renderer/pencil.cpp

,

brush.cpp

) call named_color for atomic color values and build pattern brushes for compound ones.

The converse operations are get_hex_color (which drops the alpha byte when it is 255), named_rgb_color and get_named_rgb_color. named_color_to_xcolormap returns the xcolor option ("xcolor", "x11names", "svgnames", "dvipsnames" or "texmacs") which defines a color name; the LaTeX converter uses it to declare the right color model (

convert/latex/tmtex.scm

). xpm_color is a separate parser for the colors of XPM images (only #rgb, #rrggbb, #rrrrggggbbbb, none and X11 names), used when loading XPM icons (

Graphics/Pictures/picture.cpp

).

3.Color primitives and Scheme access

Three primitives compute colors in documents (

Typeset/Env/env_exec.cpp

); all of them return hexadecimal strings:

rgb-color

(exec_rgb_color) builds a color from red, green, blue and optionally alpha components.

rgb-access

(exec_rgb_access) extracts one component of a color, selected by r, g, b, a or 0 to 3.

blend

(exec_blend) composes the first color over the second one with blend_colors.

Animations interpolate colors componentwise in morph_color (

Typeset/Env/env_animate.cpp

).

From Scheme, the glue (

Scheme/Glue/build-glue-basic.scm

) exports (color name) (named_color, returning the color word as an integer), get-hex-color, named-color->xcolormap, rgba->named-color and named-color->rgba.

4.Reverse colors

The option -r (-reverse) of the command line calls set_reverse_colors (true): the whole display is shown in a “dark” variant in which each color is replaced by reverse (r, g, b). This function keeps the hue, replaces the mean level \(t\) of the three components by \(255 - t\), and rescales the deviations from the mean so that the relative saturation is preserved; black becomes white and saturated red becomes #ff8080.

The reversal is applied in two places, and both are needed to understand the values one sees:

  1. rgb_color reverses its arguments before packing them, and get_rgb_color reverses again when unpacking. The color word therefore holds the displayed color, while the components seen by the program are (approximately) the logical ones.

  2. The Qt conversions to_qcolor and to_color (

    Plugins/Qt/qt_utilities.cpp

    ) and the glyph and picture drawing code (

    qt_renderer.cpp

    ,

    qt_picture.cpp

    ) reverse the logical components once more before handing them to Qt.

Since reverse is not exactly an involution, components read back through get_rgb_color may differ from those passed to rgb_color: in reverse mode, rgb_color (255, 0, 0) reads back as \((255, 1, 1)\).

5.Floating point colors

true_color (

Graphics/Colors/true_color.hpp

) stores the four components as doubles between 0 and 1, in the fields r, g, b and a. It converts implicitly from and to color (with rounding), and is the pixel type of the raster pictures on which the effects operate (see raster pictures and effects). It provides:

The three dimensional objects also use true_color to compute lighting (see the algebra library and three dimensional objects).

6.Pitfalls

Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.1 or any later version published by the Free Software Foundation; with no Invariant Sections, with no Front-Cover Texts, and with no Back-Cover Texts. A copy of the license is included in the section entitled "GNU Free Documentation License".