![]() |
Colors | ![]() |
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.
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,
hexadecimal notations #rgb, #rgba, #rrggbb and #rrggbbaa;
names from five dictionaries, looked up in this order: the TeXmacs colors (
tm_colors.hpp
: red, dark red, pastel blue, broken white, …), the X11 colors (
x11_colors.hpp
, including gray0 to gray100 and spellings with and without spaces), the SVG/HTML colors, the LaTeX xcolor base colors and the dvips colors (given in CMYK and converted by cmyk_color).
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
).
Three primitives compute colors in documents (
Typeset/Env/env_exec.cpp
); all of them return hexadecimal strings:
(exec_rgb_color) builds a color from red, green, blue and optionally alpha components.
(exec_rgb_access) extracts one component of a color, selected by r, g, b, a or 0 to 3.
(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.
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:
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.
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)\).
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:
componentwise arithmetic (+, -, *, /, scalar multiplication), min, max, normalize (clamping to \([0, 1]\)) and hypot;
alpha handling: mul_alpha and div_alpha (premultiplied form and back), apply_alpha, copy_alpha, clear_alpha;
composition operators source_over (the second color over the first one), towards_source and alpha_distance, used by the composition effects;
weighted mixtures mix of two or four colors, in premultiplied form (
true_color.cpp
);
color transformations returned as unary_function<true_color,true_color>: color_matrix_function (a \(4 \times 5\) matrix acting on \((r, g, b, a, 1)\), used by eff-color-matrix), make_transparent_function and make_opaque_function.
The three dimensional objects also use true_color to compute lighting (see the algebra library and three dimensional objects).
rgb-color with three arguments makes TeXmacs abort, and with four arguments ignores the alpha component. The test in exec_rgb_color is inverted (
Typeset/Env/env_exec.cpp:1904
):
tree t4= (N(t)==4? tree ("255"): exec (t[3])); |
so with three arguments the missing fourth child is read. Verified at run time: (rgb-color "255" "0" "0" "128") gives #FF0000, and (rgb-color "255" "0" "0") terminates the program with an uncaught exception.
blend_colors computes the resulting alpha as \((b_A (255 - f_A) + f_A^2) / 255\) instead of \((b_A (255 - f_A) + 255 f_A) / 255\) (
Graphics/Colors/colors.cpp:159
), so blending a translucent color over an opaque one gives a translucent result. Verified at run time: (blend "#ff000080" "#0000ff") gives #80007FBF instead of an opaque #80007F.
reverse (r, g, b) uses the integer mean \(t\) and an unbounded saturation factor (colors.cpp:119-136). For nearly grey colors, the rounding error of \(t\) is amplified: in reverse mode #fefefd (almost white) is displayed as \((129, 129, 2)\), a dark yellow, instead of almost black.
max (true_color, true_color) takes the minimum of the green and alpha components (true_color.hpp:130-131).
operator *= (true_color&, const true_color&) prints both operands on the standard output each time it is called (
true_color.hpp:96
).
Unknown color names silently become black, which also makes is_color_name the only reliable way to validate user input.
The special syntax grayn in color_from_name is dead code: the test s (1,4) == "gray" compares a three character substring with a four character string (
colors.cpp:353
). The names still work because the X11 dictionary defines gray0 to gray100.
colors.hpp
declares get_xpm_color, get_cmyk_color and the variable reverse_color, none of which is defined, and defines the five dictionary hash tables as static, so that every file including the header gets its own empty copies; the accessors x11_color, svg_color, … only work inside
colors.cpp
.