diff options
| author | Chong Yidong | 2011-02-21 01:03:36 -0500 |
|---|---|---|
| committer | Chong Yidong | 2011-02-21 01:03:36 -0500 |
| commit | 6d7132563c23774dddcd825797a29ce7ae94253a (patch) | |
| tree | 28a2b790f0a182a17c3936d087b008b47086239d | |
| parent | 6b483b66430254ac219305874dce0ee15ab09eda (diff) | |
| download | emacs-6d7132563c23774dddcd825797a29ce7ae94253a.tar.gz emacs-6d7132563c23774dddcd825797a29ce7ae94253a.zip | |
Merge some code from hexrgb.el into color.el.
* lisp/color.el (color-name-to-rgb): Rename from color-rgb->normalize.
Autoload. Add optional arg FRAME, and pass it to color-values.
(color-complement): Caller changed. Doc fix.
(color-gradient): Rewrite for better clarity and efficiency.
(color-rgb-to-hex): Rename from color-rgb->hex.
(color-rgb-to-hsv): Rename from color-rgb->hsv. Force hue and
saturation to zero if the value is too small.
(color-rgb-to-hsl): Rename from color-rgb->hsl.
(color-srgb-to-xyz): Rename from color-srgb->xyz. Doc fix.
(color-xyz-to-srgb): Rename from color-xyz->srgb. Doc fix.
(color-xyz-to-lab): Rename from color-xyz->lab. Doc fix.
(color-lab-to-xyz): Rename from color-lab->xyz. Doc fix.
(color-lab-to-srgb): Rename from color-lab->srgb. Doc fix.
(color-cie-de2000): Doc fix.
* lisp/facemenu.el (color-rgb-to-hsv): Deleted; use the version in
lisp/color.el instead.
(list-colors-sort-key, list-colors-print): Use
color-normalized-values.
* lisp/faces.el (color-values): Use cond for clarity. Doc fix.
* lisp/gnus/shr-color.el (shr-color->hexadecimal): Use renamed
function names color-rgb-to-hex, color-name-to-rgb,
color-srgb-to-lab, and color-lab-to-srgb.
| -rw-r--r-- | lisp/ChangeLog | 28 | ||||
| -rw-r--r-- | lisp/color.el | 176 | ||||
| -rw-r--r-- | lisp/facemenu.el | 28 | ||||
| -rw-r--r-- | lisp/faces.el | 30 | ||||
| -rw-r--r-- | lisp/gnus/ChangeLog | 6 | ||||
| -rw-r--r-- | lisp/gnus/shr-color.el | 14 |
6 files changed, 170 insertions, 112 deletions
diff --git a/lisp/ChangeLog b/lisp/ChangeLog index 11b63fec16b..4c09a79203d 100644 --- a/lisp/ChangeLog +++ b/lisp/ChangeLog | |||
| @@ -1,3 +1,31 @@ | |||
| 1 | 2011-02-21 Chong Yidong <cyd@stupidchicken.com> | ||
| 2 | |||
| 3 | * color.el (color-name-to-rgb): Rename from color-rgb->normalize. | ||
| 4 | Autoload. Add optional arg FRAME, and pass it to color-values. | ||
| 5 | (color-complement): Caller changed. Doc fix. | ||
| 6 | (color-gradient): Rewrite for better clarity and efficiency. | ||
| 7 | |||
| 8 | * faces.el (color-values): Use cond for clarity. Doc fix. | ||
| 9 | |||
| 10 | * facemenu.el (color-rgb-to-hsv): Deleted; use the version in | ||
| 11 | color.el instead. | ||
| 12 | (list-colors-sort-key, list-colors-print): Use | ||
| 13 | color-normalized-values. | ||
| 14 | |||
| 15 | 2011-02-20 Drew Adams <drew.adams@oracle.com> | ||
| 16 | |||
| 17 | * color.el: First part of merge from hexrgb.el. | ||
| 18 | (color-rgb-to-hex): Rename from color-rgb->hex. | ||
| 19 | (color-rgb-to-hsv): Rename from color-rgb->hsv. Force hue and | ||
| 20 | saturation to zero if the value is too small. | ||
| 21 | (color-rgb-to-hsl): Rename from color-rgb->hsl. | ||
| 22 | (color-srgb-to-xyz): Rename from color-srgb->xyz. Doc fix. | ||
| 23 | (color-xyz-to-srgb): Rename from color-xyz->srgb. Doc fix. | ||
| 24 | (color-xyz-to-lab): Rename from color-xyz->lab. Doc fix. | ||
| 25 | (color-lab-to-xyz): Rename from color-lab->xyz. Doc fix. | ||
| 26 | (color-lab-to-srgb): Rename from color-lab->srgb. Doc fix. | ||
| 27 | (color-cie-de2000): Doc fix. | ||
| 28 | |||
| 1 | 2011-02-20 Alan Mackenzie <acm@muc.de> | 29 | 2011-02-20 Alan Mackenzie <acm@muc.de> |
| 2 | 30 | ||
| 3 | * progmodes/cc-cmds.el (c-beginning-of-statement): Avoid loop in | 31 | * progmodes/cc-cmds.el (c-beginning-of-statement): Avoid loop in |
diff --git a/lisp/color.el b/lisp/color.el index 3874e33bfbb..5b67eb58a63 100644 --- a/lisp/color.el +++ b/lisp/color.el | |||
| @@ -1,9 +1,10 @@ | |||
| 1 | ;;; color.el --- Color manipulation laboratory routines -*- coding: utf-8; -*- | 1 | ;;; color.el --- Color manipulation library -*- coding: utf-8; -*- |
| 2 | 2 | ||
| 3 | ;; Copyright (C) 2010-2011 Free Software Foundation, Inc. | 3 | ;; Copyright (C) 2010-2011 Free Software Foundation, Inc. |
| 4 | 4 | ||
| 5 | ;; Author: Julien Danjou <julien@danjou.info> | 5 | ;; Authors: Julien Danjou <julien@danjou.info> |
| 6 | ;; Keywords: html | 6 | ;; Drew Adams <drew.adams@oracle.com> |
| 7 | ;; Keywords: lisp, faces, color, hex, rgb, hsv, hsl, cie-lab, background | ||
| 7 | 8 | ||
| 8 | ;; This file is part of GNU Emacs. | 9 | ;; This file is part of GNU Emacs. |
| 9 | 10 | ||
| @@ -22,7 +23,13 @@ | |||
| 22 | 23 | ||
| 23 | ;;; Commentary: | 24 | ;;; Commentary: |
| 24 | 25 | ||
| 25 | ;; This package provides color manipulation functions. | 26 | ;; This package provides functions for manipulating colors, including |
| 27 | ;; converting between color representations, computing color | ||
| 28 | ;; complements, and computing CIEDE2000 color distances. | ||
| 29 | ;; | ||
| 30 | ;; Supported color representations include RGB (red, green, blue), HSV | ||
| 31 | ;; (hue, saturation, value), HSL (hue, saturation, luminence), sRGB, | ||
| 32 | ;; CIE XYZ, and CIE L*a*b* color components. | ||
| 26 | 33 | ||
| 27 | ;;; Code: | 34 | ;;; Code: |
| 28 | 35 | ||
| @@ -34,15 +41,31 @@ | |||
| 34 | (unless (boundp 'float-pi) | 41 | (unless (boundp 'float-pi) |
| 35 | (defconst float-pi (* 4 (atan 1)) "The value of Pi (3.1415926...)."))) | 42 | (defconst float-pi (* 4 (atan 1)) "The value of Pi (3.1415926...)."))) |
| 36 | 43 | ||
| 37 | (defun color-rgb->hex (red green blue) | 44 | ;;;###autoload |
| 38 | "Return hexadecimal notation for RED GREEN BLUE color. | 45 | (defun color-name-to-rgb (color &optional frame) |
| 39 | RED GREEN BLUE must be values between 0 and 1 inclusively." | 46 | "Convert COLOR string to a list of normalized RGB components. |
| 47 | COLOR should be a color name (e.g. \"white\") or an RGB triplet | ||
| 48 | string (e.g. \"#ff12ec\"). | ||
| 49 | |||
| 50 | Normally the return value is a list of three floating-point | ||
| 51 | numbers, (RED GREEN BLUE), each between 0.0 and 1.0 inclusive. | ||
| 52 | |||
| 53 | Optional arg FRAME specifies the frame where the color is to be | ||
| 54 | displayed. If FRAME is omitted or nil, use the selected frame. | ||
| 55 | If FRAME cannot display COLOR, return nil." | ||
| 56 | (mapcar (lambda (x) (/ x 65535.0)) (color-values color frame))) | ||
| 57 | |||
| 58 | (defun color-rgb-to-hex (red green blue) | ||
| 59 | "Return hexadecimal notation for the color RED GREEN BLUE. | ||
| 60 | RED GREEN BLUE must be numbers between 0.0 and 1.0 inclusive." | ||
| 40 | (format "#%02x%02x%02x" | 61 | (format "#%02x%02x%02x" |
| 41 | (* red 255) (* green 255) (* blue 255))) | 62 | (* red 255) (* green 255) (* blue 255))) |
| 42 | 63 | ||
| 43 | (defun color-complement (color) | 64 | (defun color-complement (color-name) |
| 44 | "Return the color that is the complement of COLOR." | 65 | "Return the color that is the complement of COLOR-NAME. |
| 45 | (let ((color (color-rgb->normalize color))) | 66 | COLOR-NAME should be a string naming a color (e.g. \"white\"), or |
| 67 | a string specifying a color's RGB components (e.g. \"#ff12ec\")." | ||
| 68 | (let ((color (color-name-to-rgb color-name))) | ||
| 46 | (list (- 1.0 (car color)) | 69 | (list (- 1.0 (car color)) |
| 47 | (- 1.0 (cadr color)) | 70 | (- 1.0 (cadr color)) |
| 48 | (- 1.0 (caddr color))))) | 71 | (- 1.0 (caddr color))))) |
| @@ -52,50 +75,62 @@ RED GREEN BLUE must be values between 0 and 1 inclusively." | |||
| 52 | The color list builds a color gradient starting at color START to | 75 | The color list builds a color gradient starting at color START to |
| 53 | color STOP. It does not include the START and STOP color in the | 76 | color STOP. It does not include the START and STOP color in the |
| 54 | resulting list." | 77 | resulting list." |
| 55 | (loop for i from 1 to step-number | 78 | (let* ((r (nth 0 start)) |
| 56 | with red-step = (/ (- (car stop) (car start)) (1+ step-number)) | 79 | (g (nth 1 start)) |
| 57 | with green-step = (/ (- (cadr stop) (cadr start)) (1+ step-number)) | 80 | (b (nth 2 start)) |
| 58 | with blue-step = (/ (- (caddr stop) (caddr start)) (1+ step-number)) | 81 | (r-step (/ (- (nth 0 stop) r) (1+ step-number))) |
| 59 | collect (list | 82 | (g-step (/ (- (nth 1 stop) g) (1+ step-number))) |
| 60 | (+ (car start) (* i red-step)) | 83 | (b-step (/ (- (nth 2 stop) b) (1+ step-number))) |
| 61 | (+ (cadr start) (* i green-step)) | 84 | result) |
| 62 | (+ (caddr start) (* i blue-step))))) | 85 | (dotimes (n step-number) |
| 86 | (push (list (setq r (+ r r-step)) | ||
| 87 | (setq g (+ g g-step)) | ||
| 88 | (setq b (+ b b-step))) | ||
| 89 | result)) | ||
| 90 | (nreverse result))) | ||
| 63 | 91 | ||
| 64 | (defun color-complement-hex (color) | 92 | (defun color-complement-hex (color) |
| 65 | "Return the color that is the complement of COLOR, in hexadecimal format." | 93 | "Return the color that is the complement of COLOR, in hexadecimal format." |
| 66 | (apply 'color-rgb->hex (color-complement color))) | 94 | (apply 'color-rgb-to-hex (color-complement color))) |
| 67 | 95 | ||
| 68 | (defun color-rgb->hsv (red green blue) | 96 | (defun color-rgb-to-hsv (red green blue) |
| 69 | "Convert RED GREEN BLUE values to HSV representation. | 97 | "Convert RED, GREEN, and BLUE color components to HSV. |
| 70 | Hue is in radians. Saturation and values are between 0 and 1 | 98 | RED, GREEN, and BLUE should each be numbers between 0.0 and 1.0, |
| 71 | inclusively." | 99 | inclusive. Return a list (HUE, SATURATION, VALUE), where HUE is |
| 72 | (let* ((r (float red)) | 100 | in radians and both SATURATION and VALUE are between 0.0 and 1.0, |
| 101 | inclusive." | ||
| 102 | (let* ((r (float red)) | ||
| 73 | (g (float green)) | 103 | (g (float green)) |
| 74 | (b (float blue)) | 104 | (b (float blue)) |
| 75 | (max (max r g b)) | 105 | (max (max r g b)) |
| 76 | (min (min r g b))) | 106 | (min (min r g b))) |
| 77 | (list | 107 | (if (< (- max min) 1e-8) |
| 78 | (/ (* 2 float-pi | 108 | (list 0.0 0.0 0.0) |
| 79 | (cond ((and (= r g) (= g b)) 0) | 109 | (list |
| 80 | ((and (= r max) | 110 | (/ (* 2 float-pi |
| 81 | (>= g b)) | 111 | (cond ((and (= r g) (= g b)) 0) |
| 82 | (* 60 (/ (- g b) (- max min)))) | 112 | ((and (= r max) |
| 83 | ((and (= r max) | 113 | (>= g b)) |
| 84 | (< g b)) | 114 | (* 60 (/ (- g b) (- max min)))) |
| 85 | (+ 360 (* 60 (/ (- g b) (- max min))))) | 115 | ((and (= r max) |
| 86 | ((= max g) | 116 | (< g b)) |
| 87 | (+ 120 (* 60 (/ (- b r) (- max min))))) | 117 | (+ 360 (* 60 (/ (- g b) (- max min))))) |
| 88 | ((= max b) | 118 | ((= max g) |
| 89 | (+ 240 (* 60 (/ (- r g) (- max min))))))) | 119 | (+ 120 (* 60 (/ (- b r) (- max min))))) |
| 90 | 360) | 120 | ((= max b) |
| 91 | (if (= max 0) | 121 | (+ 240 (* 60 (/ (- r g) (- max min))))))) |
| 92 | 0 | 122 | 360) |
| 93 | (- 1 (/ min max))) | 123 | (if (= max 0) 0 (- 1 (/ min max))) |
| 94 | (/ max 255.0)))) | 124 | (/ max 255.0))))) |
| 95 | 125 | ||
| 96 | (defun color-rgb->hsl (red green blue) | 126 | (defun color-rgb-to-hsl (red green blue) |
| 97 | "Convert RED GREEN BLUE colors to their HSL representation. | 127 | "Convert RED GREEN BLUE colors to their HSL representation. |
| 98 | RED, GREEN and BLUE must be between 0 and 1 inclusively." | 128 | RED, GREEN, and BLUE should each be numbers between 0.0 and 1.0, |
| 129 | inclusive. | ||
| 130 | |||
| 131 | Return a list (HUE, SATURATION, LUMINENCE), where HUE is in radians | ||
| 132 | and both SATURATION and LUMINENCE are between 0.0 and 1.0, | ||
| 133 | inclusive." | ||
| 99 | (let* ((r red) | 134 | (let* ((r red) |
| 100 | (g green) | 135 | (g green) |
| 101 | (b blue) | 136 | (b blue) |
| @@ -104,13 +139,13 @@ RED, GREEN and BLUE must be between 0 and 1 inclusively." | |||
| 104 | (delta (- max min)) | 139 | (delta (- max min)) |
| 105 | (l (/ (+ max min) 2.0))) | 140 | (l (/ (+ max min) 2.0))) |
| 106 | (list | 141 | (list |
| 107 | (if (= max min) | 142 | (if (< (- max min) 1e-8) |
| 108 | 0 | 143 | 0 |
| 109 | (* 2 float-pi | 144 | (* 2 float-pi |
| 110 | (/ (cond ((= max r) | 145 | (/ (cond ((= max r) |
| 111 | (+ (/ (- g b) delta) (if (< g b) 6 0))) | 146 | (+ (/ (- g b) delta) (if (< g b) 6 0))) |
| 112 | ((= max g) | 147 | ((= max g) |
| 113 | (+ (/ (- b r) delta) 2)) | 148 | (+ (/ (- b r) delta) 2)) |
| 114 | (t | 149 | (t |
| 115 | (+ (/ (- r g) delta) 4))) | 150 | (+ (/ (- r g) delta) 4))) |
| 116 | 6))) | 151 | 6))) |
| @@ -121,9 +156,9 @@ RED, GREEN and BLUE must be between 0 and 1 inclusively." | |||
| 121 | (/ delta (+ max min)))) | 156 | (/ delta (+ max min)))) |
| 122 | l))) | 157 | l))) |
| 123 | 158 | ||
| 124 | (defun color-srgb->xyz (red green blue) | 159 | (defun color-srgb-to-xyz (red green blue) |
| 125 | "Converts RED GREEN BLUE colors from the sRGB color space to CIE XYZ. | 160 | "Convert RED GREEN BLUE colors from the sRGB color space to CIE XYZ. |
| 126 | RED, BLUE and GREEN must be between 0 and 1 inclusively." | 161 | RED, BLUE and GREEN must be between 0 and 1, inclusive." |
| 127 | (let ((r (if (<= red 0.04045) | 162 | (let ((r (if (<= red 0.04045) |
| 128 | (/ red 12.95) | 163 | (/ red 12.95) |
| 129 | (expt (/ (+ red 0.055) 1.055) 2.4))) | 164 | (expt (/ (+ red 0.055) 1.055) 2.4))) |
| @@ -137,8 +172,8 @@ RED, BLUE and GREEN must be between 0 and 1 inclusively." | |||
| 137 | (+ (* 0.21266729 r) (* 0.7151522 g) (* 0.0721750 b)) | 172 | (+ (* 0.21266729 r) (* 0.7151522 g) (* 0.0721750 b)) |
| 138 | (+ (* 0.0193339 r) (* 0.1191920 g) (* 0.9503041 b))))) | 173 | (+ (* 0.0193339 r) (* 0.1191920 g) (* 0.9503041 b))))) |
| 139 | 174 | ||
| 140 | (defun color-xyz->srgb (X Y Z) | 175 | (defun color-xyz-to-srgb (X Y Z) |
| 141 | "Converts CIE X Y Z colors to sRGB color space." | 176 | "Convert CIE X Y Z colors to sRGB color space." |
| 142 | (let ((r (+ (* 3.2404542 X) (* -1.5371385 Y) (* -0.4985314 Z))) | 177 | (let ((r (+ (* 3.2404542 X) (* -1.5371385 Y) (* -0.4985314 Z))) |
| 143 | (g (+ (* -0.9692660 X) (* 1.8760108 Y) (* 0.0415560 Z))) | 178 | (g (+ (* -0.9692660 X) (* 1.8760108 Y) (* 0.0415560 Z))) |
| 144 | (b (+ (* 0.0556434 X) (* -0.2040259 Y) (* 1.0572252 Z)))) | 179 | (b (+ (* 0.0556434 X) (* -0.2040259 Y) (* 1.0572252 Z)))) |
| @@ -158,10 +193,10 @@ RED, BLUE and GREEN must be between 0 and 1 inclusively." | |||
| 158 | (defconst color-cie-ε (/ 216 24389.0)) | 193 | (defconst color-cie-ε (/ 216 24389.0)) |
| 159 | (defconst color-cie-κ (/ 24389 27.0)) | 194 | (defconst color-cie-κ (/ 24389 27.0)) |
| 160 | 195 | ||
| 161 | (defun color-xyz->lab (X Y Z &optional white-point) | 196 | (defun color-xyz-to-lab (X Y Z &optional white-point) |
| 162 | "Converts CIE XYZ to CIE L*a*b*. | 197 | "Convert CIE XYZ to CIE L*a*b*. |
| 163 | WHITE-POINT can be specified as (X Y Z) white point to use. If | 198 | WHITE-POINT specifies the (X Y Z) white point for the |
| 164 | none is set, `color-d65-xyz' is used." | 199 | conversion. If omitted or nil, use `color-d65-xyz'." |
| 165 | (destructuring-bind (Xr Yr Zr) (or white-point color-d65-xyz) | 200 | (destructuring-bind (Xr Yr Zr) (or white-point color-d65-xyz) |
| 166 | (let* ((xr (/ X Xr)) | 201 | (let* ((xr (/ X Xr)) |
| 167 | (yr (/ Y Yr)) | 202 | (yr (/ Y Yr)) |
| @@ -180,10 +215,10 @@ none is set, `color-d65-xyz' is used." | |||
| 180 | (* 500 (- fx fy)) ; a | 215 | (* 500 (- fx fy)) ; a |
| 181 | (* 200 (- fy fz)))))) ; b | 216 | (* 200 (- fy fz)))))) ; b |
| 182 | 217 | ||
| 183 | (defun color-lab->xyz (L a b &optional white-point) | 218 | (defun color-lab-to-xyz (L a b &optional white-point) |
| 184 | "Converts CIE L*a*b* to CIE XYZ. | 219 | "Convert CIE L*a*b* to CIE XYZ. |
| 185 | WHITE-POINT can be specified as (X Y Z) white point to use. If | 220 | WHITE-POINT specifies the (X Y Z) white point for the |
| 186 | none is set, `color-d65-xyz' is used." | 221 | conversion. If omitted or nil, use `color-d65-xyz'." |
| 187 | (destructuring-bind (Xr Yr Zr) (or white-point color-d65-xyz) | 222 | (destructuring-bind (Xr Yr Zr) (or white-point color-d65-xyz) |
| 188 | (let* ((fy (/ (+ L 16) 116.0)) | 223 | (let* ((fy (/ (+ L 16) 116.0)) |
| 189 | (fz (- fy (/ b 200.0))) | 224 | (fz (- fy (/ b 200.0))) |
| @@ -201,21 +236,18 @@ none is set, `color-d65-xyz' is used." | |||
| 201 | (* yr Yr) ; Y | 236 | (* yr Yr) ; Y |
| 202 | (* zr Zr))))) ; Z | 237 | (* zr Zr))))) ; Z |
| 203 | 238 | ||
| 204 | (defun color-srgb->lab (red green blue) | 239 | (defun color-srgb-to-lab (red green blue) |
| 205 | "Converts RGB to CIE L*a*b*." | 240 | "Convert RGB to CIE L*a*b*." |
| 206 | (apply 'color-xyz->lab (color-srgb->xyz red green blue))) | 241 | (apply 'color-xyz-to-lab (color-srgb-to-xyz red green blue))) |
| 207 | |||
| 208 | (defun color-rgb->normalize (color) | ||
| 209 | "Normalize a RGB color to values between 0 and 1 inclusively." | ||
| 210 | (mapcar (lambda (x) (/ x 65535.0)) (x-color-values color))) | ||
| 211 | 242 | ||
| 212 | (defun color-lab->srgb (L a b) | 243 | (defun color-lab-to-srgb (L a b) |
| 213 | "Converts CIE L*a*b* to RGB." | 244 | "Convert CIE L*a*b* to RGB." |
| 214 | (apply 'color-xyz->srgb (color-lab->xyz L a b))) | 245 | (apply 'color-xyz-to-srgb (color-lab-to-xyz L a b))) |
| 215 | 246 | ||
| 216 | (defun color-cie-de2000 (color1 color2 &optional kL kC kH) | 247 | (defun color-cie-de2000 (color1 color2 &optional kL kC kH) |
| 217 | "Computes the CIEDE2000 color distance between COLOR1 and COLOR2. | 248 | "Return the CIEDE2000 color distance between COLOR1 and COLOR2. |
| 218 | Colors must be in CIE L*a*b* format." | 249 | Both COLOR1 and COLOR2 should be in CIE L*a*b* format, as |
| 250 | returned by `color-srgb-to-lab' or `color-xyz-to-lab'." | ||
| 219 | (destructuring-bind (L₁ a₁ b₁) color1 | 251 | (destructuring-bind (L₁ a₁ b₁) color1 |
| 220 | (destructuring-bind (L₂ a₂ b₂) color2 | 252 | (destructuring-bind (L₂ a₂ b₂) color2 |
| 221 | (let* ((kL (or kL 1)) | 253 | (let* ((kL (or kL 1)) |
diff --git a/lisp/facemenu.el b/lisp/facemenu.el index e76e920a9f0..360383aa32b 100644 --- a/lisp/facemenu.el +++ b/lisp/facemenu.el | |||
| @@ -463,25 +463,6 @@ These special properties include `invisible', `intangible' and `read-only'." | |||
| 463 | 463 | ||
| 464 | (defalias 'facemenu-read-color 'read-color) | 464 | (defalias 'facemenu-read-color 'read-color) |
| 465 | 465 | ||
| 466 | (defun color-rgb-to-hsv (r g b) | ||
| 467 | "For R, G, B color components return a list of hue, saturation, value. | ||
| 468 | R, G, B input values should be in [0..65535] range. | ||
| 469 | Output values for hue are integers in [0..360] range. | ||
| 470 | Output values for saturation and value are integers in [0..100] range." | ||
| 471 | (let* ((r (/ r 65535.0)) | ||
| 472 | (g (/ g 65535.0)) | ||
| 473 | (b (/ b 65535.0)) | ||
| 474 | (max (max r g b)) | ||
| 475 | (min (min r g b)) | ||
| 476 | (h (cond ((= max min) 0) | ||
| 477 | ((= max r) (mod (+ (* 60 (/ (- g b) (- max min))) 360) 360)) | ||
| 478 | ((= max g) (+ (* 60 (/ (- b r) (- max min))) 120)) | ||
| 479 | ((= max b) (+ (* 60 (/ (- r g) (- max min))) 240)))) | ||
| 480 | (s (cond ((= max 0) 0) | ||
| 481 | (t (- 1 (/ min max))))) | ||
| 482 | (v max)) | ||
| 483 | (list (round h) (round s 0.01) (round v 0.01)))) | ||
| 484 | |||
| 485 | (defcustom list-colors-sort nil | 466 | (defcustom list-colors-sort nil |
| 486 | "Color sort order for `list-colors-display'. | 467 | "Color sort order for `list-colors-display'. |
| 487 | `nil' means default implementation-dependent order (defined in `x-colors'). | 468 | `nil' means default implementation-dependent order (defined in `x-colors'). |
| @@ -508,6 +489,7 @@ and excludes grayscale colors." | |||
| 508 | "Return a list of keys for sorting colors depending on `list-colors-sort'. | 489 | "Return a list of keys for sorting colors depending on `list-colors-sort'. |
| 509 | COLOR is the name of the color. When return value is nil, | 490 | COLOR is the name of the color. When return value is nil, |
| 510 | filter out the color from the output." | 491 | filter out the color from the output." |
| 492 | (require 'color) | ||
| 511 | (cond | 493 | (cond |
| 512 | ((null list-colors-sort) color) | 494 | ((null list-colors-sort) color) |
| 513 | ((eq list-colors-sort 'name) | 495 | ((eq list-colors-sort 'name) |
| @@ -517,12 +499,12 @@ filter out the color from the output." | |||
| 517 | ((eq (car-safe list-colors-sort) 'rgb-dist) | 499 | ((eq (car-safe list-colors-sort) 'rgb-dist) |
| 518 | (color-distance color (cdr list-colors-sort))) | 500 | (color-distance color (cdr list-colors-sort))) |
| 519 | ((eq list-colors-sort 'hsv) | 501 | ((eq list-colors-sort 'hsv) |
| 520 | (apply 'color-rgb-to-hsv (color-values color))) | 502 | (apply 'color-rgb-to-hsv (color-name-to-rgb color))) |
| 521 | ((eq (car-safe list-colors-sort) 'hsv-dist) | 503 | ((eq (car-safe list-colors-sort) 'hsv-dist) |
| 522 | (let* ((c-rgb (color-values color)) | 504 | (let* ((c-rgb (color-name-to-rgb color)) |
| 523 | (c-hsv (apply 'color-rgb-to-hsv c-rgb)) | 505 | (c-hsv (apply 'color-rgb-to-hsv c-rgb)) |
| 524 | (o-hsv (apply 'color-rgb-to-hsv | 506 | (o-hsv (apply 'color-rgb-to-hsv |
| 525 | (color-values (cdr list-colors-sort))))) | 507 | (color-name-to-rgb (cdr list-colors-sort))))) |
| 526 | (unless (and (eq (nth 0 c-rgb) (nth 1 c-rgb)) ; exclude grayscale | 508 | (unless (and (eq (nth 0 c-rgb) (nth 1 c-rgb)) ; exclude grayscale |
| 527 | (eq (nth 1 c-rgb) (nth 2 c-rgb))) | 509 | (eq (nth 1 c-rgb) (nth 2 c-rgb))) |
| 528 | ;; 3D Euclidean distance (sqrt is not needed for sorting) | 510 | ;; 3D Euclidean distance (sqrt is not needed for sorting) |
| @@ -638,7 +620,7 @@ You can change the color sort order by customizing `list-colors-sort'." | |||
| 638 | 'mouse-face 'highlight | 620 | 'mouse-face 'highlight |
| 639 | 'help-echo | 621 | 'help-echo |
| 640 | (let ((hsv (apply 'color-rgb-to-hsv | 622 | (let ((hsv (apply 'color-rgb-to-hsv |
| 641 | (color-values (car color))))) | 623 | (color-name-to-rgb (car color))))) |
| 642 | (format "H:%d S:%d V:%d" | 624 | (format "H:%d S:%d V:%d" |
| 643 | (nth 0 hsv) (nth 1 hsv) (nth 2 hsv))))) | 625 | (nth 0 hsv) (nth 1 hsv) (nth 2 hsv))))) |
| 644 | (when callback | 626 | (when callback |
diff --git a/lisp/faces.el b/lisp/faces.el index 11c4108644a..b49cdc777be 100644 --- a/lisp/faces.el +++ b/lisp/faces.el | |||
| @@ -1653,18 +1653,28 @@ If COLOR is the symbol `unspecified' or one of the strings | |||
| 1653 | 1653 | ||
| 1654 | (defun color-values (color &optional frame) | 1654 | (defun color-values (color &optional frame) |
| 1655 | "Return a description of the color named COLOR on frame FRAME. | 1655 | "Return a description of the color named COLOR on frame FRAME. |
| 1656 | The value is a list of integer RGB values--(RED GREEN BLUE). | 1656 | COLOR should be a string naming a color (e.g. \"white\"), or a |
| 1657 | These values appear to range from 0 to 65280 or 65535, depending | 1657 | string specifying a color's RGB components (e.g. \"#ff12ec\"). |
| 1658 | on the system; white is \(65280 65280 65280\) or \(65535 65535 65535\). | 1658 | |
| 1659 | Return a list of three integers, (RED GREEN BLUE), each between 0 | ||
| 1660 | and either 65280 or 65535 (the maximum depends on the system). | ||
| 1661 | Use `color-name-to-rgb' if you want RGB floating-point values | ||
| 1662 | normalized to 1.0. | ||
| 1663 | |||
| 1659 | If FRAME is omitted or nil, use the selected frame. | 1664 | If FRAME is omitted or nil, use the selected frame. |
| 1660 | If FRAME cannot display COLOR, the value is nil. | 1665 | If FRAME cannot display COLOR, the value is nil. |
| 1661 | If COLOR is the symbol `unspecified' or one of the strings | 1666 | |
| 1662 | \"unspecified-fg\" or \"unspecified-bg\", the value is nil." | 1667 | COLOR can also be the symbol `unspecified' or one of the strings |
| 1663 | (if (member color '(unspecified "unspecified-fg" "unspecified-bg")) | 1668 | \"unspecified-fg\" or \"unspecified-bg\", in which case the |
| 1664 | nil | 1669 | return value is nil." |
| 1665 | (if (memq (framep (or frame (selected-frame))) '(x w32 ns)) | 1670 | (cond |
| 1666 | (xw-color-values color frame) | 1671 | ((member color '(unspecified "unspecified-fg" "unspecified-bg")) |
| 1667 | (tty-color-values color frame)))) | 1672 | nil) |
| 1673 | ((memq (framep (or frame (selected-frame))) '(x w32 ns)) | ||
| 1674 | (xw-color-values color frame)) | ||
| 1675 | (t | ||
| 1676 | (tty-color-values color frame)))) | ||
| 1677 | |||
| 1668 | (defalias 'x-color-values 'color-values) | 1678 | (defalias 'x-color-values 'color-values) |
| 1669 | 1679 | ||
| 1670 | (declare-function xw-display-color-p "xfns.c" (&optional terminal)) | 1680 | (declare-function xw-display-color-p "xfns.c" (&optional terminal)) |
diff --git a/lisp/gnus/ChangeLog b/lisp/gnus/ChangeLog index 671b1b02603..a4bbfbf48e4 100644 --- a/lisp/gnus/ChangeLog +++ b/lisp/gnus/ChangeLog | |||
| @@ -1,3 +1,9 @@ | |||
| 1 | 2011-02-20 Chong Yidong <cyd@stupidchicken.com> | ||
| 2 | |||
| 3 | * shr-color.el (shr-color->hexadecimal): Use renamed function names | ||
| 4 | color-rgb-to-hex, color-name-to-rgb, color-srgb-to-lab, and | ||
| 5 | color-lab-to-srgb. | ||
| 6 | |||
| 1 | 2011-02-19 Glenn Morris <rgm@gnu.org> | 7 | 2011-02-19 Glenn Morris <rgm@gnu.org> |
| 2 | 8 | ||
| 3 | * gnus.el (gnus-meta): Doc fix. | 9 | * gnus.el (gnus-meta): Doc fix. |
diff --git a/lisp/gnus/shr-color.el b/lisp/gnus/shr-color.el index 4b885d9dbf8..36dd65f4a2d 100644 --- a/lisp/gnus/shr-color.el +++ b/lisp/gnus/shr-color.el | |||
| @@ -259,7 +259,7 @@ Like rgb() or hsl()." | |||
| 259 | (l (/ (string-to-number (match-string-no-properties 3 color)) 100.0))) | 259 | (l (/ (string-to-number (match-string-no-properties 3 color)) 100.0))) |
| 260 | (destructuring-bind (r g b) | 260 | (destructuring-bind (r g b) |
| 261 | (shr-color-hsl-to-rgb-fractions h s l) | 261 | (shr-color-hsl-to-rgb-fractions h s l) |
| 262 | (color-rgb->hex r g b)))) | 262 | (color-rgb-to-hex r g b)))) |
| 263 | ;; Color names | 263 | ;; Color names |
| 264 | ((cdr (assoc-string color shr-color-html-colors-alist t))) | 264 | ((cdr (assoc-string color shr-color-html-colors-alist t))) |
| 265 | ;; Unrecognized color :( | 265 | ;; Unrecognized color :( |
| @@ -325,13 +325,13 @@ If FIXED-BACKGROUND is set, and if the color are not visible, a | |||
| 325 | new background color will not be computed. Only the foreground | 325 | new background color will not be computed. Only the foreground |
| 326 | color will be adapted to be visible on BG." | 326 | color will be adapted to be visible on BG." |
| 327 | ;; Convert fg and bg to CIE Lab | 327 | ;; Convert fg and bg to CIE Lab |
| 328 | (let ((fg-norm (color-rgb->normalize fg)) | 328 | (let ((fg-norm (color-name-to-rgb fg)) |
| 329 | (bg-norm (color-rgb->normalize bg))) | 329 | (bg-norm (color-name-to-rgb bg))) |
| 330 | (if (or (null fg-norm) | 330 | (if (or (null fg-norm) |
| 331 | (null bg-norm)) | 331 | (null bg-norm)) |
| 332 | (list bg fg) | 332 | (list bg fg) |
| 333 | (let* ((fg-lab (apply 'color-srgb->lab fg-norm)) | 333 | (let* ((fg-lab (apply 'color-srgb-to-lab fg-norm)) |
| 334 | (bg-lab (apply 'color-srgb->lab bg-norm)) | 334 | (bg-lab (apply 'color-srgb-to-lab bg-norm)) |
| 335 | ;; Compute color distance using CIE DE 2000 | 335 | ;; Compute color distance using CIE DE 2000 |
| 336 | (fg-bg-distance (color-cie-de2000 fg-lab bg-lab)) | 336 | (fg-bg-distance (color-cie-de2000 fg-lab bg-lab)) |
| 337 | ;; Compute luminance distance (substract L component) | 337 | ;; Compute luminance distance (substract L component) |
| @@ -351,10 +351,10 @@ color will be adapted to be visible on BG." | |||
| 351 | bg | 351 | bg |
| 352 | (apply 'format "#%02x%02x%02x" | 352 | (apply 'format "#%02x%02x%02x" |
| 353 | (mapcar (lambda (x) (* (max (min 1 x) 0) 255)) | 353 | (mapcar (lambda (x) (* (max (min 1 x) 0) 255)) |
| 354 | (apply 'color-lab->srgb bg-lab)))) | 354 | (apply 'color-lab-to-srgb bg-lab)))) |
| 355 | (apply 'format "#%02x%02x%02x" | 355 | (apply 'format "#%02x%02x%02x" |
| 356 | (mapcar (lambda (x) (* (max (min 1 x) 0) 255)) | 356 | (mapcar (lambda (x) (* (max (min 1 x) 0) 255)) |
| 357 | (apply 'color-lab->srgb fg-lab)))))))))) | 357 | (apply 'color-lab-to-srgb fg-lab)))))))))) |
| 358 | 358 | ||
| 359 | (provide 'shr-color) | 359 | (provide 'shr-color) |
| 360 | 360 | ||