GNU bug report logs - #14074
24.3.50; Convert hsv to rgb

Previous Next

Package: emacs;

Reported by: Jambunathan K <kjambunathan <at> gmail.com>

Date: Thu, 28 Mar 2013 15:00:02 UTC

Severity: wishlist

Found in version 24.3.50

Done: Jambunathan K <kjambunathan <at> gmail.com>

Bug is archived. No further changes may be made.

To add a comment to this bug, you must first unarchive it, by sending
a message to control AT debbugs.gnu.org, with unarchive 14074 in the body.
You can then email your comments to 14074 AT debbugs.gnu.org in the normal way.

Toggle the display of automated, internal messages from the tracker.

View this report as an mbox folder, status mbox, maintainer mbox


Report forwarded to bug-gnu-emacs <at> gnu.org:
bug#14074; Package emacs. (Thu, 28 Mar 2013 15:00:02 GMT) Full text and rfc822 format available.

Acknowledgement sent to Jambunathan K <kjambunathan <at> gmail.com>:
New bug report received and forwarded. Copy sent to bug-gnu-emacs <at> gnu.org. (Thu, 28 Mar 2013 15:00:02 GMT) Full text and rfc822 format available.

Message #5 received at submit <at> debbugs.gnu.org (full text, mbox):

From: Jambunathan K <kjambunathan <at> gmail.com>
To: bug-gnu-emacs <at> gnu.org
Subject: 24.3.50; Convert hsv to rgb
Date: Thu, 28 Mar 2013 20:26:15 +0530
[Message part 1 (text/plain, inline)]
Convert hsv to rgb.

Here is a little test case.

    (let ((color-name (format "#%06x" (random #xffffff))))
      (format "color-name: %s  round-tripped: %s"
              color-name
              (apply 'color-rgb-to-hex 
                     (apply 'color-hsv-to-rgb 
                            (apply 'color-rgb-to-hsv 
                                   (color-name-to-rgb color-name))))))

    => "color-name: #01043d  round-tripped: #01033d"
    => "color-name: #1278e2  round-tripped: #1278e2"
    => "color-name: #ffb4bd  round-tripped: #ffb4bd"

One can use `color-hsv-to-rgb' for example in `vc-annotate-color-map'.

[Message part 2 (text/x-diff, inline)]
=== modified file 'lisp/ChangeLog'
*** lisp/ChangeLog	2013-03-27 16:03:15 +0000
--- lisp/ChangeLog	2013-03-28 14:36:48 +0000
***************
*** 1,3 ****
--- 1,7 ----
+ 2013-03-28  Jambunathan K  <kjambunathan <at> gmail.com>
+ 
+ 	* color.el (color-hsv-to-rgb): New defun.
+ 
  2013-03-27  Eli Zaretskii  <eliz <at> gnu.org>
  
  	* facemenu.el (list-colors-callback): New defvar.

=== modified file 'lisp/color.el'
*** lisp/color.el	2013-01-11 15:04:24 +0000
--- lisp/color.el	2013-03-28 14:40:31 +0000
***************
*** 118,123 ****
--- 118,148 ----
    "Return the color that is the complement of COLOR, in hexadecimal format."
    (apply 'color-rgb-to-hex (color-complement color)))
  
+ (defun color-hsv-to-rgb (h s v)
+   "Convert HSV color components to RGB.
+ HUE should be in radians between 0 and 2*`float-pi', inclusive.
+ SATURATION and VALUE should be between 0.0 and 1.0, inclusive.
+ Return a list (RED GREEN BLUE) where each of the components is
+ between 0.0 and 1.0, inclusive."
+   ;; Convert h from radians to degrees
+   (setq h (/ (* 180.0 h) float-pi))
+   (when (>= h 360) (setq h 0.0))
+   (if (zerop s)
+       (list v v v)
+     (let* ((h (/ h 60.0))
+ 	   (i (floor h))
+ 	   (f (- h i))
+ 	   (l (* v (- 1 s)))
+ 	   (m (* v (- 1 (* s f))))
+ 	   (n (* v (- 1 (* s (- 1 f))))))
+       (cond
+        ((= i 0) (list v n l))
+        ((= i 1) (list m v l))
+        ((= i 2) (list l v n))
+        ((= i 3) (list l m v))
+        ((= i 4) (list n l v))
+        ((= i 5) (list v l m))))))
+ 
  (defun color-rgb-to-hsv (red green blue)
    "Convert RGB color components to HSV.
  RED, GREEN, and BLUE should each be numbers between 0.0 and 1.0,

[Message part 3 (text/plain, inline)]

In GNU Emacs 24.3.50.1 (i686-pc-linux-gnu, GTK+ Version 2.20.1)
 of 2013-03-28 on debian-6.05
Bzr revision: 112161 eliz <at> gnu.org-20130327160315-f4jh29xefzj11qgu
Windowing system distributor `The X.Org Foundation', version 11.0.10707000
Important settings:
  value of $LANG: en_IN
  locale-coding-system: iso-latin-1-unix
  default enable-multibyte-characters: t




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#14074; Package emacs. (Sun, 07 Apr 2013 13:42:02 GMT) Full text and rfc822 format available.

Message #8 received at 14074 <at> debbugs.gnu.org (full text, mbox):

From: Jambunathan K <kjambunathan <at> gmail.com>
To: 14074 <at> debbugs.gnu.org
Subject: Re: bug#14074: 24.3.50; Convert hsv to rgb
Date: Sun, 07 Apr 2013 19:07:44 +0530
[Message part 1 (text/plain, inline)]
(Check my copyright assignment status before applying the patch provided
in my earlier mail.)


Jambunathan K <kjambunathan <at> gmail.com> 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:

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

[copy-of-suggest-colors-use-pure-color.png (image/png, attachment)]
[copy-of-suggest-colors-plain.png (image/png, attachment)]

Reply sent to Jambunathan K <kjambunathan <at> gmail.com>:
You have taken responsibility. (Fri, 15 Nov 2013 04:09:02 GMT) Full text and rfc822 format available.

Notification sent to Jambunathan K <kjambunathan <at> gmail.com>:
bug acknowledged by developer. (Fri, 15 Nov 2013 04:09:03 GMT) Full text and rfc822 format available.

Message #13 received at 14074-done <at> debbugs.gnu.org (full text, mbox):

From: Jambunathan K <kjambunathan <at> gmail.com>
To: 14074-done <at> debbugs.gnu.org
Subject: Re: bug#14074: 24.3.50; Convert hsv to rgb
Date: Tue, 01 Jan 2002 06:11:23 +0530
The snippet I shared is not usable within Emacs.  As OP closing it.




bug archived. Request was from Debbugs Internal Request <help-debbugs <at> gnu.org> to internal_control <at> debbugs.gnu.org. (Fri, 13 Dec 2013 12:24:28 GMT) Full text and rfc822 format available.

This bug report was last modified 11 years and 193 days ago.

Previous Next


GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.