%ents; Red/Green-Blindness"> Blue-Blindness"> ]>
Consistent Color Generation This specification provides a set of algorithms to consistently generate colors given a string. The string can be a nickname, a JID or any other piece of information. All entities adhering to this specification generate the same color for the same string, which provides a consistent user experience across platforms. &LEGALNOTICE; 0392 Experimental Standards Track Standards Council colors &jonaswielicki; 0.8.0 2021-10-26 jsc

Remove Color Vision Deficiency correction algorithms and substitute them with a better recommendation.

0.7.0 2019-10-16 jsc

Remove anything except the Hue generation from the specification. The other parts were user interface suggestions which are out of scope for the XSF standards process.

0.6.0 2018-10-02 jsc

Fix CVD rules: they were incorrect in the last update.

Update test vectors after CVD fix.

Add note about floating-point modulo operator implementation interoperability.

0.5 2018-10-01 jsc

Switch from custom YCbCr-based algorithm to HSLuv.

Prioritize bare JIDs over nicknames.

Write down normalization rules.

0.4.1 2018-07-28 egp

Fix a typo. (thanks zinid)

0.4 2017-11-29 jwi

Use different formulas for Color Vision Deficiency correction, as suggested by Marcus Waldvogel.

Update test vectors.

Clarify generation of the angle.

Prioritize nicknames over bare JIDs.

Add rationale for new palette mapping algorithm introduced in 0.3.

0.3 2017-11-13 jwi

Fix wording in angle generation section which did still use CRC32. Rework palette mapping after with implementation experience.

0.2 2017-10-04 jwi

Move to SHA-1 as mixing function; Properly reference BT.601 and include constants in text; Prefer bare JID over roster name when selecting the hash function input; Editing.

0.1 2017-09-27 XEP Editor: jwi

Accepted as Experimental by Council.

0.0.1 2017-09-14 jwi

First draft.

Colors provide a valuable visual cue to recognize shapes. Recognition of colors works much faster than recognition of text. Together with the length and overall shape of a piece of text (such as a nickname), a color provides a decent amount of entropy to distinguish a reasonable amount of entities, without having to actually read the text.

Clients have been using randomly or deterministically chosen colors for users in multi-user situations for a long time already. However, since there has been no standard for how this is implemented, the experience differs across platforms. The goal of this XEP is to provide a uniform, platform-independent, stateless and easy-to-implement way to map arbitrary bytestrings to colors.

To allow cross-client use, it is important that the color scheme can be adapted to different environments. This specification provides means to adapt colors to different background colors as well as &cvds;.

In no way is the system presented in this specification a replacement for names. It only serves as an additional visual aid.

The color generation mechanism should provide the following features:

To generate a color from a string of text, the following algorithms are applied in order:

  1. Generate a Hue value from the text.
  2. If constraints mandate the use of only a small palette of colors, map the angle to the closest palette color. (Such situations could for example be a UI environment with guidelines to only use a specific set of colors or an output device which only supports a limited amount of colors.)
  3. If the output device supports RGB output, Convert the angle to a RGB.

The algorithms in this document use the &hsluv; color space. It provides consistent brightness (for a given luminosity) across its entire definition space. There is also widespread library support.

Input: An identifier, encoded as octets of UTF-8 (&rfc3269;).

Output: Hue angle.

Note: The goal of this algorithm is to convert arbitrary text into a scalar value which can then be used to calculate a color.

  1. Run the input through SHA-1 (&rfc3174;).
  2. Treat the output as little endian and extract the least-significant 16 bits. (These are the first two bytes of the output, with the second byte being the most significant one.)
  3. Divide the value by 65536 (use float division) and multiply it by 360 (to map it to degrees in a full circle).

Use the HSLuv operation hsluvToRgb to convert the Hue angle to a color. The saturation and lightness are to be defined by the implementation (see also the Contrast Ratio considerations).

Input: A set of RGB colors (each component from 0 to 1).

Output: A mapping from angles (integer, from 0 to 360) to RGB colors.

Note: when the algorithm finishes, the mapping maps angles (rounded to two decimal places) to the R, G, B triples which come closest to the desired color and lightness.

  1. Create an empty mapping M which maps from Hue angles to quadruples of L, R, G and B.
  2. For each color R, G, B from the input palette:
    1. If the R, G and B values are equal, skip the color and continue with the next. (Grayscale does not work well, since its saturation and hue are undefined.)
    2. Calculate H, S and L from R, G, B using HSLuv.
    3. Round the angle to the next integer value.
    4. If the angle is not in the mapping M yet, or if the L value of the existing entry is farther away from 73.2 than the new L value, put the L, R, G, and B values as value for the angle into the mapping.
  3. Strip the L values from the values of mapping M.
  4. Return M as the result of the algorithm.

Implementations are free to choose a representation for palette colors different from R, G, B triplets. The exact representation does not matter, as long as it can be converted to a Hue angle accordingly.

Input: (a) A mapping which maps angles to R, G, B triplets and (b) a color to map to the closest palette color as angle alpha.

Output: A palette color as R, G, B triplet.

Note: See Conversion of an RGB color palette to a Hue palette on how to convert an R, G, B triplet to an angle.

  1. First, check if alpha rounded to an integer. If so, return that match immediately.
  2. For each angle beta in the palette, calculate the distance metric: D = min((alpha - beta) % 360, (beta - alpha) % 360).
  3. Return the R, G, B triplet associated with the angle with the smallest distance metric D.

Implementations are free to choose a representation for palette colors different from R, G, B triplets. The exact representation does not matter, as long as it can be converted to a Hue angle accordingly.

Implementations should be aware of Gamma correction and apply it as needed.

When processing JIDs as text input, implementations MUST prepare the JID as it would for comparing it to another JID with a case-sensitive comparison function.

This specification describes the generation of colors for strings. Users with color vision deficiencies may have a lower range of distinguishable colors. Implementations should observe the usual recommendations regarding the use of color in that regard.

Some users may find a huge variety of colors on their screen distracting. Any implementation making use of this color generation algorithm should support replacing all generated colors with a static, potentially user-configurable, color.

Implementations should adapt the lightness value according to the background on which the color is rendered in order to achieve a good contrast ratio.

This specification extracts a bit more information from an entity and shows it alongside the existing information to the user. As the algorithm is likely to produce different colors for look-alikes (see &xep0165; for examples) in JIDs, it may add additional protection against attacks based on those.

Due to the limited set of distinguishable colors and only extracting 16 bits of the hash function output, possible &cvds; and/or use of palettes, entities MUST NOT rely on colors being unique in any context.

This section provides an overview of design considerations made while writing this specification. It shows alternatives which have been considered, and eventually rejected.

The versions up to 0.5 of this document used a variant of the YCbCr color space (namely &BT.601;) along with a custom algorithm to convert from angles to CbCr and from there to RGB. The HSLuv color space provides extremely consistent apparent brightness of the colors which cannot be achieved with simple application of YCbCr. In addition, HSLuv has widespread library support.

The HSV and HSL color spaces fail to provide uniform luminosity with fixed value/lightness and saturation parameters. Adapting those parameters for uniform luminosity across the hue range would have complicated the algorithm with litte to no gain.

Given a fixed-size and finite palette of colors, it would be possible to ensure that, until the number of entities to color exceeds the number of colors, no color collisions happen.

There are issues with this approach when the set of entities is dynamic. In such cases, it is possible that an entity changes its associated color (for example by re-joining a colored group chat), which defeats the original purpose.

In addition, more state needs to be taken into account, increasing the complexity of choosing a color.

This specification needs to collapse an arbitrarily long string into just a few bits (the angle in the CbCr plane). To do so, SHA-1 (&rfc3174;) is used.

CRC32 and Adler32 have been considered as faster alternatives. Downsides of these functions:

  • Bad mixing without additional entropy.
  • Adler32 is rarely available in standard libraries.
  • CRC32 is ambiguous: there are multiple polynomials in widespread use (e.g. the Ethernet and the zlib polynomials). Often it is not clear which polynomial is used by a library.

SHA-1 is widely available. From a security point of view, the exact choice of hash function does not matter here, since it is truncated to 16 bits. At this length, any cryptographic hash function is weak.

The palette-mapping algorithm operates on angles only and disregards the lightness value except if the angles match. This has the downside that the brightness is not equal over the range of the palette mapped colors.

The alternative would be to require the lightness to be close to the target lightness. This has several issues:

  • We cannot know if a palette can satisfy the given lightness at all.
  • Many colors from e.g. the "Web Safe" palette (used in 256 color terminals and the test vectors) will not satisfy any given lightness, reducing the size of the effective palette drastically.

For the sake of having more colors available, the given algorithm was chosen which prefers many colors with hue conformance over fewer colors with hue and lightness conformance.

An earlier version of this spec included a makeshift algorithm to correct for &cvds;. However, this was considered suboptimal for the following reasons:

  • Any operating system level &cvd; correction is likely to produce better results.
  • The actual benefit from an accessibility point of view is unclear, given that the &cvd; itself already reduces the dynamic range; if the &cvd; correction algorithm and the &cvd; are not fully in tune about the range reduction, unnecessary additional loss of information may occur.
  • The algorithms introduced significant complexity on some platforms.

Future versions of this spec may re-introduce recommendations if especially the second point can be refuted by credible sources.

This document requires no interaction with &IANA;.

This document requires no interaction with the ®ISTRAR;.

Thanks to Klaus Herberth, Daniel Gultsch, Georg Lukas, Tobias Markmann, Christian Schudt, and Marcus Waldvogel for their input and feedback on this document.

This section holds test vectors. The test vectors are provided as Comma Separated Values. Strings are enclosed by single quotes ('). The first line contains a header. Each row contains, in that order, the original text, the text encoded as UTF-8 as hexadecimal octets, the angle in degrees, the calculated hue in degrees (hue and angle are the same as of version 0.8.0), and the Red, Green, and Blue values.

The used palette can be generated by sampling the RGB cube evenly with six samples on each axis (resulting in 210 colors (grayscales are excluded)). The resulting palette is commonly known as the palette of so-called "Web Safe" colors.