unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
* bug#14074: 24.3.50; Convert hsv to rgb
  2013-03-28 14:56 bug#14074: 24.3.50; Convert hsv to rgb Jambunathan K
@ 2002-01-01  0:41 ` Jambunathan K
  2013-04-07 13:37 ` Jambunathan K
  1 sibling, 0 replies; 3+ messages in thread
From: Jambunathan K @ 2002-01-01  0:41 UTC (permalink / raw)
  To: 14074-done


The snippet I shared is not usable within Emacs.  As OP closing it.





^ permalink raw reply	[flat|nested] 3+ messages in thread

* bug#14074: 24.3.50; Convert hsv to rgb
@ 2013-03-28 14:56 Jambunathan K
  2002-01-01  0:41 ` Jambunathan K
  2013-04-07 13:37 ` Jambunathan K
  0 siblings, 2 replies; 3+ messages in thread
From: Jambunathan K @ 2013-03-28 14:56 UTC (permalink / raw)
  To: 14074

[-- Attachment #1: Type: text/plain, Size: 630 bytes --]


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'.


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: color.el.diff --]
[-- Type: text/x-diff, Size: 1666 bytes --]

=== 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@gmail.com>
+ 
+ 	* color.el (color-hsv-to-rgb): New defun.
+ 
  2013-03-27  Eli Zaretskii  <eliz@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,


[-- Attachment #3: Type: text/plain, Size: 366 bytes --]



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@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




^ permalink raw reply	[flat|nested] 3+ messages in thread

* bug#14074: 24.3.50; Convert hsv to rgb
  2013-03-28 14:56 bug#14074: 24.3.50; Convert hsv to rgb Jambunathan K
  2002-01-01  0:41 ` Jambunathan K
@ 2013-04-07 13:37 ` Jambunathan K
  1 sibling, 0 replies; 3+ messages in thread
From: Jambunathan K @ 2013-04-07 13:37 UTC (permalink / raw)
  To: 14074

[-- Attachment #1: Type: text/plain, Size: 9279 bytes --]


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


Jambunathan K <kjambunathan@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:

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


[-- Attachment #2: copy-of-suggest-colors-use-pure-color.png --]
[-- Type: image/png, Size: 245385 bytes --]

[-- Attachment #3: copy-of-suggest-colors-plain.png --]
[-- Type: image/png, Size: 197645 bytes --]

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2013-04-07 13:37 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-03-28 14:56 bug#14074: 24.3.50; Convert hsv to rgb Jambunathan K
2002-01-01  0:41 ` Jambunathan K
2013-04-07 13:37 ` Jambunathan K

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).