Subject: bug#14074: 24.3.50; Convert hsv to rgb

From: Jambunathan K <[email-masked]>
Date: Sun, 07 Apr 2013 19:07:44 +0530

(Check my copyright assignment status before applying the patch provided
in my earlier mail.)

Jambunathan K <[email-masked]> writes:
> Convert hsv to rgb.

Here are a few additions/enhancements to color.el and facemenu.el that
would be useful.  These suggestions are based on an prototype that I
built.  

----------------------------------------------------------------

M-x suggest-colors RET
======================

To get a feel for the proposed
        M-x suggest-colors RET

see the attached screenshots, particulary one named
copy-of-suggest-colors-use-pure-color.png.  The only difference between
the two screenshots is that one works off the base/pure color while the
other works off the given color.

See next section for additional comments.

----------------------------------------------------------------

Table of Contents
─────────────────

1 Enhancements to color.el
2 Enhancements to facemenu.el
3 Add M-x suggest-colors RET

1 Enhancements to color.el
══════════════════════════

  1. Clarify difference between rgb and srgb.  (Frankly, I don't know
     the difference.)

     Here are some references found.
     1. [http://debbugs.gnu.org/cgi/bugreport.cgi?bug=8402]
     2. [http://www.w3.org/Graphics/Color/sRGB.html]
     Audit the usage of "srgb" in `color-lab-to-srgb',
     `color-lab-to-srgb' etc and check whether the terms rgb (#rrggbb)
     to srgb are used consistently.

     See also note on Item 7.

  2. Rgb to hsv conversion

     ╭────
     │ (defun color-hsv-to-rgb (h s v))
     ╰────

  3. Add two colors

     ╭────
     │ (defun color-add-hsl-or-hsv (h s v-or-l δh δs δv-or-δl))
     ╰────

     Use `color-lighten-hsl', `color-saturate-hsl' & co. can be
     re-written to use this API.

  4. Mod/Round-off a color, for use with (2) above

     ╭────
     │ (defun color-mod (value value-max))
     ╰────

     The function will use modulo operation - C-h f mod.  Contrast this
     with `color-clamp'.

  5. APIs to rotate a color

     ╭────
     │ (defun color-rotate-hsl-or-hsv (H S L-OR-V degrees))
     │ (defun color-rotate-name (name degrees))
     ╰────

  6. Get base or pure color (I am not sure about the terminology)

     ╭────
     │ (defun color-pure-color (name))
     ╰────

     This API may not be needed.

  7. API for computing relative luminance of a given color.

     ╭────
     │ (defun color-relative-luminance (name))
     ╰────

     Quoting from [http://www.w3.org/TR/WCAG20/]
     ╭────
     │ relative luminance
     │ 
     │ The relative brightness of any point in a colorspace, normalized to 0
     │ or darkest black and 1 for lightest white
     │ 
     │ Note 1: For the sRGB colorspace, the relative luminance of a color is
     │ defined as L = 0.2126 * R + 0.7152 * G + 0.0722 * B where R, G and B are
     │ defined as:
     │ 
     │     * if RsRGB <= 0.03928 then R = RsRGB/12.92 else R = ((RsRGB+0.055)/1.055) ^ 2.4
     │     * if GsRGB <= 0.03928 then G = GsRGB/12.92 else G = ((GsRGB+0.055)/1.055) ^ 2.4
     │     * if BsRGB <= 0.03928 then B = BsRGB/12.92 else B = ((BsRGB+0.055)/1.055) ^ 2.4
     │ 
     │ and RsRGB, GsRGB, and BsRGB are defined as:
     │ 
     │     * RsRGB = R8bit/255
     │     * GsRGB = G8bit/255
     │     * BsRGB = B8bit/255
     │ 
     │ The "^" character is the exponentiation operator. (Formula taken from [sRGB] and [IEC-4WD]).
     │ 
     │ Note 2: Almost all systems used today to view Web content assume sRGB
     │ encoding. Unless it is known that another color space will be used to
     │ process and display the content, authors should evaluate using sRGB
     │ colorspace. If using other color spaces, see Understanding Success
     │ Criterion 1.4.3.
     ╰────

     Additional note: In `list-colors-sort-key' (search for luminance),
     above linear relation for L is being used, without the associated
     conditional checks suggested above.

  8. An API for computing luminosity contrast ratio between two colors.

     ╭────
     │ (defun luminosity-contrast-ratio (name1 name2))
     ╰────

     This will help in selecting a foreground and background colors
     (presumably non-grey) which provide sufficient contrast.

     See [http://www.w3.org/TR/WCAG20/#contrast-ratiodef].

     ╭────
     │ 1.4.3 Contrast (Minimum): The visual presentation of text and images
     │ of text has a contrast ratio of at least 4.5:1, except for the
     │ following: (Level AA)
     ╰────

2 Enhancements to facemenu.el
═════════════════════════════

  1. Enhance M-x display-colors RET to *also* show hsv value.

     Currently hsv values are shown as tooltip.  The format specifier
     for printing the hsv values is *really not that useful*.

     See attached screenshots.

  2. Indicate darkness or lightness of the color (based on value of
     relative luminance).

     Useful to quickly settle on white or black as a contrasting color.

  3. Add "Sort by contrast (against a chosen color)" to C-h v
     list-colors-sort RET.

     This can be used to find colors (presumably non-grey) that will
     contrast nicely with given color.

  4. Refactor `list-colors-display'.

     M-x `list-colors-display' does two things - sorting and displaying.

     I find that the displaying part is awkwardly split between this
     routine and `list-colors-print'. (Note that the *Help* buffer
     generated in the former and displaying done in the later.  This is
     not very useful in practice.)

     So, split `list-colors-display' to say `sort-colors' and
     `display-colors'.

  5. Find a color name that is nearest to a given numeric color.

     ╭────
     │ (defun nearest-color (color))
     ╰────

     A naive implementation would return, for example,

     ╭────
     │ (car (sort-colors list-of-colors)) ; pseudo-code
     ╰────

  6. Use the standards based `color-cie-de2000' to compute differences
     between colors and do away with `hsv-dist' and `rgb-dist'.

     Currently, sorting by `hsv-dist' uses Euclidean distance over
     cylindrical co-ordinates.  My little experimentation shows that
     `hsv-dist' as it is defined is satisfactory for finding the nearest
     color name to a given numericla color.

     `rgb-dist' distance is less than satisfactory in some edge cases.
     So, it's claim that it is closer to human perception is a bit
     questionable.

  7. Re-think `color-gradient' in terms of color scheme (see next
     section)

     ╭────
     │ (defun color-gradient (start stop step-number)
     ╰────

     May be use a hsv or hsl based scheme rather than a rgb based scheme
     for computing gradients.

3 Add M-x suggest-colors RET
════════════════════════════

  This command can provide following options.  Note that each of these
  sets is generated by doing a "set of deltas" to the base color.

  1. complements :: Color that is 180° apart

     ╭────
     │ complement(c) = c + [180°, 0, 0]
     ╰────

  1. split complements :: Colors that are on either side of a
     complementary color

     ╭────
     │ split complement(c) = {c, c + [180°-δdeg, 0, 0], c + [180°+δdeg, 0, 0]}, δdeg = 30°
     ╰────

  1. triads :: Colors that are 120° apart

     ╭────
     │ triads(c) = {c, c + [120°, 0, 0], c + [240°, 0, 0]}
     ╰────

  2. tetrads :: Colors that are 90° apart

     ╭────
     │ tetrads(c) = {c, c + [90°, 0, 0], c + [180°, 0, 0], c + [270°, 0, 0]}
     ╰────

  3. Analogous :: Colors that are equidistant and closer to a given
     color.

     ╭────
     │ analogous(c) = {c, c + [30°, 0, 0], c + [-30°, 0, 0]}
     ╰────

  4. Monochromatic :: Colors having same hue but different saturation
     and value.  (Tint, tone and shade etc)

     ╭────
     │ mono(c)  = {c, c + [0°, 0.3, 0], c + [0°, 0, 0.3], c + [0°, 0.3, 0.3]}
     ╰────

  See [Color theory] which explains how [Color scheme designer] could be
  made use of.

  Interestingly, the above designer provides different sets of
  monochrome colors.  Vary choice of "Preset" menu and examine how
  Adjust schemes->Adjust variants to see how the monochrome deltas are
  located in s,v plane.

  [Color theory] http://www.w3.org/wiki/Colour_theory

  [Color scheme designer] http://colorschemedesigner.com/

;; Local Variables:
;; coding: utf-8-unix
;; End:

----------------------------------------------------------------