<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type='text/xsl' href='xep.xsl'?>
<xep xmlns="">
<header>
  <title>Consistent Color Generation</title>
  <abstract>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.</abstract>
  
<legal>
<copyright>This XMPP Extension Protocol is copyright © 1999 – 2024 by the <link url="https://xmpp.org/">XMPP Standards Foundation</link> (XSF).</copyright>
<permissions>Permission is hereby granted, free of charge, to any person obtaining a copy of this specification (the "Specification"), to make use of the Specification without restriction, including without limitation the rights to implement the Specification in a software program, deploy the Specification in a network service, and copy, modify, merge, publish, translate, distribute, sublicense, or sell copies of the Specification, and to permit persons to whom the Specification is furnished to do so, subject to the condition that the foregoing copyright notice and this permission notice shall be included in all copies or substantial portions of the Specification. Unless separate permission is granted, modified works that are redistributed shall not contain misleading information regarding the authors, title, number, or publisher of the Specification, and shall not claim endorsement of the modified works by the authors, any organization or project to which the authors belong, or the XMPP Standards Foundation.</permissions>
<warranty>## NOTE WELL: This Specification is provided on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. ##</warranty>
<liability>In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall the XMPP Standards Foundation or any author of this Specification be liable for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising from, out of, or in connection with the Specification or the implementation, deployment, or other use of the Specification (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if the XMPP Standards Foundation or such author has been advised of the possibility of such damages.</liability>
<conformance>This XMPP Extension Protocol has been contributed in full conformance with the XSF's Intellectual Property Rights Policy (a copy of which can be found at &lt;<link url="https://xmpp.org/about/xsf/ipr-policy">https://xmpp.org/about/xsf/ipr-policy</link>&gt; or obtained by writing to XMPP Standards Foundation, P.O. Box 787, Parker, CO 80134 USA).</conformance>
</legal>
  <number>0392</number>
  <status>Draft</status>
  <lastcall>2024-03-25</lastcall>
  <type>Standards Track</type>
  <sig>Standards</sig>
  <approver>Council</approver>
  <dependencies/>
  <supersedes/>
  <supersededby/>
  <shortname>colors</shortname>
  
  <author>
    <firstname>Jonas</firstname>
    <surname>Schäfer</surname>
    <email>jonas@wielicki.name</email>
    <jid>jonas@wielicki.name</jid>
  </author>

  <revision>
    <version>1.0.1</version>
    <date>2025-10-03</date>
    <initials>egp</initials>
    <remark><p>Add missing saturation and lightness information to compute the test vectors.</p></remark>
  </revision>
  <revision>
    <version>1.0.0</version>
    <date>2024-03-27</date>
    <initials>XEP Editor (dg)</initials>
    <remark>Accept as Stable as per Council Vote from 2024-03-27.</remark>
  </revision>
  <revision>
    <version>0.8.0</version>
    <date>2021-10-26</date>
    <initials>jsc</initials>
    <remark>
      <p>Remove Color Vision Deficiency correction algorithms and substitute
        them with a better recommendation.</p>
    </remark>
  </revision>
  <revision>
    <version>0.7.0</version>
    <date>2019-10-16</date>
    <initials>jsc</initials>
    <remark>
      <p>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.</p>
    </remark>
  </revision>
  <revision>
    <version>0.6.0</version>
    <date>2018-10-02</date>
    <initials>jsc</initials>
    <remark>
      <p>Fix CVD rules: they were incorrect in the last update.</p>
      <p>Update test vectors after CVD fix.</p>
      <p>Add note about floating-point modulo operator implementation interoperability.</p>
    </remark>
  </revision>
  <revision>
    <version>0.5</version>
    <date>2018-10-01</date>
    <initials>jsc</initials>
    <remark>
      <p>Switch from custom YCbCr-based algorithm to HSLuv.</p>
      <p>Prioritize bare JIDs over nicknames.</p>
      <p>Write down normalization rules.</p>
    </remark>
  </revision>
  <revision>
    <version>0.4.1</version>
    <date>2018-07-28</date>
    <initials>egp</initials>
    <remark>
      <p>Fix a typo. (thanks zinid)</p>
    </remark>
  </revision>
  <revision>
    <version>0.4</version>
    <date>2017-11-29</date>
    <initials>jwi</initials>
    <remark>
      <p>Use different formulas for Color Vision Deficiency correction, as suggested by Marcus Waldvogel.</p>
      <p>Update test vectors.</p>
      <p>Clarify generation of the angle.</p>
      <p>Prioritize nicknames over bare JIDs.</p>
      <p>Add rationale for new palette mapping algorithm introduced in 0.3.</p>
    </remark>
  </revision>
  <revision>
    <version>0.3</version>
    <date>2017-11-13</date>
    <initials>jwi</initials>
    <remark><p>
      Fix wording in angle generation section which did still use CRC32.
      Rework palette mapping after with implementation experience.
    </p></remark>
  </revision>
  <revision>
    <version>0.2</version>
    <date>2017-10-04</date>
    <initials>jwi</initials>
    <remark><p>
        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.
    </p></remark>
  </revision>
  <revision>
    <version>0.1</version>
    <date>2017-09-27</date>
    <initials>XEP Editor: jwi</initials>
    <remark><p>Accepted as Experimental by Council.</p></remark>
  </revision>
  <revision>
    <version>0.0.1</version>
    <date>2017-09-14</date>
    <initials>jwi</initials>
    <remark><p>First draft.</p></remark>
  </revision>
</header>
<section1 topic="Introduction" anchor="intro">
  <p>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.</p>
  <p>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.</p>
  <p>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 Color Vision Deficiencies.</p>
  <p>In no way is the system presented in this specification a replacement for names. It only serves as an additional visual aid.</p>
</section1>
<section1 topic="Requirements" anchor="reqs">
  <p>The color generation mechanism should provide the following features:</p>
  <ul>
    <li>Consistent generation of color across all platforms depending solely on the identifier used as input for the algorithm.</li>
    <li>The system should be reasonably fast; it must be possible to, for example, apply it to all roster entries even of very large rosters in reasonable amount of time.</li>
    <li>It must be able to provide decent contrast on any background.</li>
    <li>The implementation should be stateless and not be complex.</li>
    <li>A fallback path for users with common types of Color Vision Deficiencies must be provided.</li>
    <li>A fallback path for systems which can only use colors from a restricted palette must be provided.</li>
  </ul>
</section1>
<section1 topic="Use Cases" anchor="usecases">
  <section2 topic="Generating a color" anchor="usecase-textcolor">
    <p>To generate a color from a string of text, the following algorithms are applied in order:</p>
    <ol>
      <li><link url="#algorithm-angle">Generate a Hue value from the text</link>.</li>
      <li>If constraints mandate the use of only a small palette of colors, <link url="#algorithm-mappalette">map the angle to the closest palette color</link>. (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.)</li>
      <li>If the output device supports RGB output, <link url="#algorithm-rgb">Convert the angle to a RGB</link>.</li>
    </ol>
  </section2>
</section1>
<section1 topic="Business Rules" anchor="rules">
  <ul>
    <li>Implementations SHOULD allow the user to turn off any colorization completely.</li>
  </ul>
</section1>
<section1 topic="Algorithms" anchor="algorithm">
  <p>The algorithms in this document use the <span class="ref"><link url="http://www.hsluv.org/">HSLuv</link></span> <note>HSLuv &lt;<link url="http://www.hsluv.org/">http://www.hsluv.org/</link>&gt;.</note> color space. It provides consistent brightness (for a given luminosity) across its entire definition space. There is also widespread library support.</p>
  <section2 topic="Angle generation" anchor="algorithm-angle">
    <p>Input: An identifier, encoded as octets of UTF-8 (<span class="ref"><link url="http://tools.ietf.org/html/rfc3269">RFC 3269</link></span> <note>RFC 3269: UTF-8, a transformation format of ISO 10646 &lt;<link url="http://tools.ietf.org/html/rfc3269">http://tools.ietf.org/html/rfc3269</link>&gt;.</note>).</p>
    <p>Output: Hue angle.</p>
    <p>Note: The goal of this algorithm is to convert arbitrary text into a scalar value which can then be used to calculate a color.</p>
    <ol>
      <li>Run the input through SHA-1 (<span class="ref"><link url="http://tools.ietf.org/html/rfc3174">RFC 3174</link></span> <note>RFC 3174: US Secure Hash Algorithm 1 (SHA1) &lt;<link url="http://tools.ietf.org/html/rfc3174">http://tools.ietf.org/html/rfc3174</link>&gt;.</note>).</li>
      <li>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.)</li>
      <li>Divide the value by 65536 (use float division) and multiply it by 360 (to map it to degrees in a full circle).</li>
    </ol>
  </section2>
  <section2 topic="RGB generation" anchor="algorithm-rgb">
    <p>Use the HSLuv operation <tt>hsluvToRgb</tt> to convert the Hue angle to a color. The saturation and lightness are to be defined by the implementation (see also the <link url="#accessibility-cr">Contrast Ratio considerations</link>).</p>
  </section2>
  <section2 topic="Conversion of an RGB color palette to a Hue palette" anchor="algorithm-genpalette">
    <p>Input: A set of RGB colors (each component from 0 to 1).</p>
    <p>Output: A mapping from angles (integer, from 0 to 360) to RGB colors.</p>
    <p>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.</p>
    <ol>
      <li>Create an empty mapping M which maps from Hue angles to quadruples of L, R, G and B.</li>
      <li>For each color R, G, B from the input palette:
        <ol>
          <li>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.)</li>
          <li>Calculate H, S and L from R, G, B using HSLuv.</li>
          <li>Round the angle to the next integer value.</li>
          <li>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.</li>
        </ol>
      </li>
      <li>Strip the L values from the values of mapping M.</li>
      <li>Return M as the result of the algorithm.</li>
    </ol>
    <p>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.</p>
  </section2>
  <section2 topic="Mapping of a Hue angle to closest palette color" anchor="algorithm-mappalette">
    <p>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.</p>
    <p>Output: A palette color as R, G, B triplet.</p>
    <p>Note: See <link url="#algorithm-genpalette">Conversion of an RGB color palette to a Hue palette</link> on how to convert an R, G, B triplet to an angle.</p>
    <ol>
      <li>First, check if alpha rounded to an integer. If so, return that match immediately.</li>
      <li>For each angle beta in the palette, calculate the distance metric: <code>D = min((alpha - beta) % 360, (beta - alpha) % 360)</code>.</li>
      <li>Return the R, G, B triplet associated with the angle with the smallest distance metric D.</li>
    </ol>
    <p>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.</p>
  </section2>
</section1>
<section1 topic="Implementation Notes" anchor="impl">
  <section2 topic="Gamma Correction" anchor="impl-gamma">
    <p>Implementations should be aware of Gamma correction and apply it as needed.</p>
  </section2>
  <section2 topic="Normalization" anchor="impl-norm">
    <p>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.</p>
  </section2>
</section1>
<section1 topic="Accessibility Considerations" anchor="access">
  <p>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.</p>
  <p>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.</p>
  <p>Implementations should adapt the lightness value according to the background on which the color is rendered in order to achieve a good contrast ratio.</p>
</section1>
<section1 topic="Security Considerations" anchor="security">
  <p>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 <span class="ref"><link url="https://xmpp.org/extensions/xep-0165.html">Best Practices to Prevent JID Mimicking (XEP-0165)</link></span> <note>XEP-0165: Best Practices to Prevent JID Mimicking &lt;<link url="https://xmpp.org/extensions/xep-0165.html">https://xmpp.org/extensions/xep-0165.html</link>&gt;.</note> for examples) in JIDs, it may add additional protection against attacks based on those.</p>
  <p>Due to the limited set of distinguishable colors and only extracting 16 bits of the hash function output, possible Color Vision Deficiencies and/or use of palettes, entities MUST NOT rely on colors being unique in any context.</p>
</section1>
<section1 topic="Design Considerations" anchor="design">
  <p>This section provides an overview of design considerations made while writing this specification. It shows alternatives which have been considered, and eventually rejected.</p>
  <section2 topic="The YCbCr color space" anchor="design-ycbcr">
    <p>The versions up to 0.5 of this document used a variant of the YCbCr color space (namely <span class="ref"><link url="https://www.itu.int/rec/R-REC-BT.601-7-201103-I/en">BT.601</link></span> <note>BT.601: Studio encoding parameters of digital television for standard 4:3 and wide screen 16:9 aspect ratios &lt;<link url="https://www.itu.int/rec/R-REC-BT.601-7-201103-I/en">https://www.itu.int/rec/R-REC-BT.601-7-201103-I/en</link>&gt;</note>) 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.</p>
  </section2>
  <section2 topic="Hue-Saturation-Value/Lightness color space" anchor="design-hsv">
    <p>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.</p>
  </section2>
  <section2 topic="Palette-based and context-aware coloring" anchor="design-context">
    <p>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.</p>
    <p>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.</p>
    <p>In addition, more state needs to be taken into account, increasing the complexity of choosing a color.</p>
  </section2>
  <section2 topic="Choice of mixing function in angle generation" anchor="design-mixing">
    <p>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 (<span class="ref"><link url="http://tools.ietf.org/html/rfc3174">RFC 3174</link></span> <note>RFC 3174: US Secure Hash Algorithm 1 (SHA1) &lt;<link url="http://tools.ietf.org/html/rfc3174">http://tools.ietf.org/html/rfc3174</link>&gt;.</note>) is used.</p>
    <p>CRC32 and Adler32 have been considered as faster alternatives. Downsides of these functions:</p>
    <ul>
      <li>Bad mixing without additional entropy.</li>
      <li>Adler32 is rarely available in standard libraries.</li>
      <li>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.</li>
    </ul>
    <p>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.</p>
  </section2>
  <section2 topic="Palette-mapping function" anchor="design-palette-mapping">
    <p>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.</p>
    <p>The alternative would be to require the lightness to be close to the target lightness. This has several issues:</p>
    <ul>
      <li>We cannot know if a palette can satisfy the given lightness at all.</li>
      <li>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.</li>
    </ul>
    <p>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.</p>
  </section2>
  <section2 topic="Color Vision Deficiency corrections" anchor="design-cvd-corr">
    <p>An earlier version of this spec included a makeshift algorithm to correct for Color Vision Deficiencies. However, this was considered suboptimal for the following reasons:</p>
    <ul>
      <li>Any operating system level Color Vision Deficiency correction is likely to produce better results.</li>
      <li>The actual benefit from an accessibility point of view is unclear, given that the Color Vision Deficiency itself already reduces the dynamic range; if the Color Vision Deficiency correction algorithm and the Color Vision Deficiency are not fully in tune about the range reduction, unnecessary additional loss of information may occur.</li>
      <li>The algorithms introduced significant complexity on some platforms.</li>
    </ul>
    <p>Future versions of this spec may re-introduce recommendations if especially the second point can be refuted by credible sources.</p>
  </section2>
</section1>
<section1 topic="IANA Considerations" anchor="iana">
  <p>This document requires no interaction with the <span class="ref"><link url="http://www.iana.org/">Internet Assigned Numbers Authority (IANA)</link></span> <note>The Internet Assigned Numbers Authority (IANA) is the central coordinator for the assignment of unique parameter values for Internet protocols, such as port numbers and URI schemes. For further information, see &lt;<link url="http://www.iana.org/">http://www.iana.org/</link>&gt;.</note>. </p>
</section1>
<section1 topic="XMPP Registrar Considerations" anchor="registrar">
  <p>This document requires no interaction with the <span class="ref"><link url="https://xmpp.org/registrar/">XMPP Registrar</link></span> <note>The XMPP Registrar maintains a list of reserved protocol namespaces as well as registries of parameters used in the context of XMPP extension protocols approved by the XMPP Standards Foundation. For further information, see &lt;<link url="https://xmpp.org/registrar/">https://xmpp.org/registrar/</link>&gt;.</note>. </p>
</section1>
<section1 topic="Acknowledgements" anchor="acknowledgements">
  <p>Thanks to Klaus Herberth, Daniel Gultsch, Georg Lukas, Tobias Markmann, Christian Schudt, and Marcus Waldvogel for their input and feedback on this document.</p>
</section1>
<section1 topic="Test Vectors" anchor="vectors">
  <section2 topic="Test Vectors" anchor="testvectors-fullrange">
    <p>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 at saturation 100 and lightness 50.</p>
    <code><![CDATA[text,hextext,angle,hue,r,g,b
'Romeo','526f6d656f',327.255249,327.255249,0.865,0.000,0.686
'juliet@capulet.lit','6a756c69657440636170756c65742e6c6974',209.410400,209.410400,0.000,0.515,0.573
'😺','f09f98ba',331.199341,331.199341,0.872,0.000,0.659
'council','636f756e63696c',359.994507,359.994507,0.918,0.000,0.394
'Board','426f617264',171.430664,171.430664,0.000,0.527,0.457]]></code>
  </section2>
  <section2 topic="Test Vectors for mapping to 216 color palette" anchor="testvectors-palette">
    <p>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.</p>
    <code><![CDATA[text,hextext,hue,best_hue,r,g,b
'Romeo','526f6d656f',327.255249,327,1.000,0.000,0.800
'juliet@capulet.lit','6a756c69657440636170756c65742e6c6974',209.410400,226,0.000,0.800,1.000
'😺','f09f98ba',331.199341,331,1.000,0.400,0.800
'council','636f756e63696c',359.994507,359,0.800,0.200,0.400
'Board','426f617264',171.430664,161,0.000,1.000,0.800]]></code>
  </section2>
</section1>
</xep>
