annotate lisp/gnus/shr-color.el @ 111678:3b6c0c4ef2bb

shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal. shr-color.el (shr-color->hexadecimal): Add converting functions for RGB() or HSL() color representation. shr.el (shr-tag-font): Add. (shr-tag-color-check): New function to get better colors. (shr-tag-insert-color-overlay): Factorize code between tag-font and tag-span. shr-color.el: New file. color-lab.el: New file.
author Katsumi Yamaoka <yamaoka@jpl.org>
date Tue, 23 Nov 2010 00:03:44 +0000
parents
children 01aefe45207c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
111678
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
1 ;;; shr-color.el --- Simple HTML Renderer color management
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
2
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
3 ;; Copyright (C) 2010 Free Software Foundation, Inc.
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
4
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
5 ;; Author: Julien Danjou <julien@danjou.info>
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
6 ;; Keywords: html
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
7
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
8 ;; This file is part of GNU Emacs.
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
9
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
11 ;; it under the terms of the GNU General Public License as published by
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
12 ;; the Free Software Foundation, either version 3 of the License, or
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
13 ;; (at your option) any later version.
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
14
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
15 ;; GNU Emacs is distributed in the hope that it will be useful,
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
18 ;; GNU General Public License for more details.
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
19
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
20 ;; You should have received a copy of the GNU General Public License
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
22
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
23 ;;; Commentary:
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
24
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
25 ;; This package handles colors display for shr.
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
26
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
27 ;;; Code:
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
28
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
29 (require 'color-lab)
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
30
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
31 (defgroup shr-color nil
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
32 "Simple HTML Renderer colors"
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
33 :group 'shr)
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
34
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
35 (defcustom shr-color-visible-luminance-min 40
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
36 "Minimum luminance distance between two colors to be considered visible.
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
37 Must be between 0 and 100."
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
38 :group 'shr
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
39 :type 'float)
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
40
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
41 (defcustom shr-color-visible-distance-min 5
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
42 "Minimum color distance between two colors to be considered visible.
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
43 This value is used to compare result for `ciede2000'. Its an
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
44 absolute value without any unit."
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
45 :group 'shr
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
46 :type 'integer)
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
47
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
48 (defun shr-color-relative-to-absolute (number)
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
49 "Convert a relative NUMBER to absolute. If NUMBER is absolute, return NUMBER.
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
50 This will convert \"80 %\" to 204, \"100 %\" to 255 but \"123\" to \"123\"."
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
51 (let ((string-length (- (length number) 1)))
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
52 ;; Is this a number with %?
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
53 (if (eq (elt number string-length) ?%)
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
54 (/ (* (string-to-number (substring number 0 string-length)) 255) 100)
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
55 (string-to-number number))))
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
56
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
57 (defun shr-color-hsl-to-rgb-fractions (h s l)
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
58 "Convert H S L to fractional RGB values."
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
59 (let (m1 m2)
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
60 (if (<= l 0.5)
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
61 (setq m2 (* l (+ s 1)))
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
62 (setq m2 (- (+ l s) (* l s))))
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
63 (setq m1 (- (* l 2) m2))
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
64 (list (rainbow-hue-to-rgb m1 m2 (+ h (/ 1 3.0)))
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
65 (rainbow-hue-to-rgb m1 m2 h)
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
66 (rainbow-hue-to-rgb m1 m2 (- h (/ 1 3.0))))))
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
67
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
68 (defun shr-color->hexadecimal (color)
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
69 "Convert any color format to hexadecimal representation.
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
70 Like rgb() or hsl()."
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
71 (when color
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
72 (cond ((or (string-match
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
73 "rgb(\s*\\([0-9]\\{1,3\\}\\(?:\s*%\\)?\\)\s*,\s*\\([0-9]\\{1,3\\}\\(?:\s*%\\)?\\)\s*,\s*\\([0-9]\\{1,3\\}\\(?:\s*%\\)?\\)\s*)"
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
74 color)
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
75 (string-match
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
76 "rgba(\s*\\([0-9]\\{1,3\\}\\(?:\s*%\\)?\\)\s*,\s*\\([0-9]\\{1,3\\}\\(?:\s*%\\)?\\)\s*,\s*\\([0-9]\\{1,3\\}\\(?:\s*%\\)?\\)\s*,\s*[0-9]*\.?[0-9]+\s*%?\s*)"
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
77 color))
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
78 (format "#%02X%02X%02X"
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
79 (shr-color-relative-to-absolute (match-string-no-properties 1 color))
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
80 (shr-color-relative-to-absolute (match-string-no-properties 2 color))
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
81 (shr-color-relative-to-absolute (match-string-no-properties 3 color))))
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
82 ((or (string-match
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
83 "hsl(\s*\\([0-9]\\{1,3\\}\\)\s*,\s*\\([0-9]\\{1,3\\}\\)\s*%\s*,\s*\\([0-9]\\{1,3\\}\\)\s*%\s*)"
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
84 color)
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
85 (string-match
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
86 "hsla(\s*\\([0-9]\\{1,3\\}\\)\s*,\s*\\([0-9]\\{1,3\\}\\)\s*%\s*,\s*\\([0-9]\\{1,3\\}\\)\s*%\s*,\s*[0-9]*\.?[0-9]+\s*%?\s*)"
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
87 color))
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
88 (let ((h (/ (string-to-number (match-string-no-properties 1 color)) 360.0))
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
89 (s (/ (string-to-number (match-string-no-properties 2 color)) 100.0))
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
90 (l (/ (string-to-number (match-string-no-properties 3 color)) 100.0)))
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
91 (destructuring-bind (r g b)
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
92 (rainbow-hsl-to-rgb-fractions h s l)
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
93 (format "#%02X%02X%02X" (* r 255) (* g 255) (* b 255)))))
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
94 (t
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
95 color))))
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
96
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
97 (defun set-minimum-interval (val1 val2 min max interval &optional fixed)
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
98 "Set minimum interval between VAL1 and VAL2 to INTERVAL.
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
99 The values are bound by MIN and MAX.
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
100 If FIXED is t, then val1 will not be touched."
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
101 (let ((diff (abs (- val1 val2))))
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
102 (unless (>= diff interval)
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
103 (if fixed
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
104 (let* ((missing (- interval diff))
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
105 ;; If val2 > val1, try to increase val2
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
106 ;; That's the "good direction"
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
107 (val2-good-direction
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
108 (if (> val2 val1)
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
109 (min max (+ val2 missing))
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
110 (max min (- val2 missing))))
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
111 (diff-val2-good-direction-val1 (abs (- val2-good-direction val1))))
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
112 (if (>= diff-val2-good-direction-val1 interval)
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
113 (setq val2 val2-good-direction)
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
114 ;; Good-direction is not so good, compute bad-direction
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
115 (let* ((val2-bad-direction
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
116 (if (> val2 val1)
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
117 (max min (- val1 interval))
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
118 (min max (+ val1 interval))))
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
119 (diff-val2-bad-direction-val1 (abs (- val2-bad-direction val1))))
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
120 (if (>= diff-val2-bad-direction-val1 interval)
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
121 (setq val2 val2-bad-direction)
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
122 ;; Still not good, pick the best and prefer good direction
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
123 (setq val2
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
124 (if (>= diff-val2-good-direction-val1 diff-val2-bad-direction-val1)
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
125 val2-good-direction
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
126 val2-bad-direction))))))
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
127 ;; No fixed, move val1 and val2
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
128 (let ((missing (/ (- interval diff) 2.0)))
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
129 (if (< val1 val2)
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
130 (setq val1 (max min (- val1 missing))
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
131 val2 (min max (+ val2 missing)))
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
132 (setq val2 (max min (- val2 missing))
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
133 val1 (min max (+ val1 missing))))
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
134 (setq diff (abs (- val1 val2))) ; Recompute diff
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
135 (unless (>= diff interval)
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
136 ;; Not ok, we hit a boundary
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
137 (let ((missing (- interval diff)))
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
138 (cond ((= val1 min)
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
139 (setq val2 (+ val2 missing)))
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
140 ((= val2 min)
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
141 (setq val1 (+ val1 missing)))
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
142 ((= val1 max)
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
143 (setq val2 (- val2 missing)))
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
144 ((= val2 max)
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
145 (setq val1 (- val1 missing)))))))))
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
146 (list val1 val2)))
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
147
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
148 (defun shr-color-visible (bg fg &optional fixed-background)
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
149 "Check that BG and FG colors are visible if they are drawn on each other.
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
150 Return t if they are. If they are too similar, two new colors are
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
151 returned instead.
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
152 If FIXED-BACKGROUND is set, and if the color are not visible, a
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
153 new background color will not be computed. Only the foreground
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
154 color will be adapted to be visible on BG."
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
155 ;; Convert fg and bg to CIE Lab
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
156 (let* ((fg-lab (apply 'rgb->lab (rgb->normalize fg)))
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
157 (bg-lab (apply 'rgb->lab (rgb->normalize bg)))
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
158 ;; Compute color distance using CIE DE 2000
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
159 (fg-bg-distance (color-lab-ciede2000 fg-lab bg-lab))
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
160 ;; Compute luminance distance (substract L component)
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
161 (luminance-distance (abs (- (car fg-lab) (car bg-lab)))))
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
162 (if (and (>= fg-bg-distance shr-color-visible-distance-min)
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
163 (>= luminance-distance shr-color-visible-luminance-min))
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
164 (list bg fg)
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
165 ;; Not visible, try to change luminance to make them visible
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
166 (let ((Ls (set-minimum-interval (car bg-lab) (car fg-lab) 0 100
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
167 shr-color-visible-luminance-min
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
168 fixed-background)))
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
169 (setcar bg-lab (car Ls))
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
170 (setcar fg-lab (cadr Ls))
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
171 (list
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
172 (apply 'format "#%02x%02x%02x"
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
173 (mapcar (lambda (x) (* (max (min 1 x) 0) 255)) (apply 'lab->rgb bg-lab)))
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
174 (apply 'format "#%02x%02x%02x"
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
175 (mapcar (lambda (x) (* (max (min 1 x) 0) 255)) (apply 'lab->rgb fg-lab))))))))
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
176
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
177 (provide 'shr-color)
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
178
3b6c0c4ef2bb shr.el (shr-tag-color-check): Convert colors to hexadecimal with shr-color->hexadecimal.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
179 ;;; shr-color.el ends here