aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSimen Heggestøyl2017-09-07 20:40:12 +0200
committerSimen Heggestøyl2017-09-13 20:23:44 +0200
commite7d6c622090dd2f4c77fbd04aba89c30a75514dd (patch)
tree775683d9e552e7add0e29ea01057aebfd62d0693
parent63398071471f6cd6b006d3c35d2d83c597549e4a (diff)
downloademacs-e7d6c622090dd2f4c77fbd04aba89c30a75514dd.tar.gz
emacs-e7d6c622090dd2f4c77fbd04aba89c30a75514dd.zip
Add tests for color.el
* lisp/color.el (color-name-to-rgb, color-complement): Clarify in docstrings that RGB triplets should use four digits per component. (color-rgb-to-hsl): Break line to avoid "Hidden behind deeper element" warning. * test/lisp/color-tests.el: New file.
-rw-r--r--lisp/color.el8
-rw-r--r--test/lisp/color-tests.el251
2 files changed, 256 insertions, 3 deletions
diff --git a/lisp/color.el b/lisp/color.el
index 22b6808c87f..e22b3cf0f6e 100644
--- a/lisp/color.el
+++ b/lisp/color.el
@@ -42,7 +42,7 @@
42(defun color-name-to-rgb (color &optional frame) 42(defun color-name-to-rgb (color &optional frame)
43 "Convert COLOR string to a list of normalized RGB components. 43 "Convert COLOR string to a list of normalized RGB components.
44COLOR should be a color name (e.g. \"white\") or an RGB triplet 44COLOR should be a color name (e.g. \"white\") or an RGB triplet
45string (e.g. \"#ff12ec\"). 45string (e.g. \"#ffff1122eecc\").
46 46
47Normally the return value is a list of three floating-point 47Normally the return value is a list of three floating-point
48numbers, (RED GREEN BLUE), each between 0.0 and 1.0 inclusive. 48numbers, (RED GREEN BLUE), each between 0.0 and 1.0 inclusive.
@@ -68,7 +68,8 @@ or 2; use the latter if you need a 24-bit specification of a color."
68(defun color-complement (color-name) 68(defun color-complement (color-name)
69 "Return the color that is the complement of COLOR-NAME. 69 "Return the color that is the complement of COLOR-NAME.
70COLOR-NAME should be a string naming a color (e.g. \"white\"), or 70COLOR-NAME should be a string naming a color (e.g. \"white\"), or
71a string specifying a color's RGB components (e.g. \"#ff12ec\")." 71a string specifying a color's RGB
72components (e.g. \"#ffff1212ecec\")."
72 (let ((color (color-name-to-rgb color-name))) 73 (let ((color (color-name-to-rgb color-name)))
73 (list (- 1.0 (nth 0 color)) 74 (list (- 1.0 (nth 0 color))
74 (- 1.0 (nth 1 color)) 75 (- 1.0 (nth 1 color))
@@ -178,7 +179,8 @@ each element is between 0.0 and 1.0, inclusive."
178 ((= r max) (- bc gc)) 179 ((= r max) (- bc gc))
179 ((= g max) (+ 2.0 rc (- bc))) 180 ((= g max) (+ 2.0 rc (- bc)))
180 (t (+ 4.0 gc (- rc)))) 181 (t (+ 4.0 gc (- rc))))
181 6.0) 1.0))) 182 6.0)
183 1.0)))
182 (list h s l))))) 184 (list h s l)))))
183 185
184(defun color-srgb-to-xyz (red green blue) 186(defun color-srgb-to-xyz (red green blue)
diff --git a/test/lisp/color-tests.el b/test/lisp/color-tests.el
new file mode 100644
index 00000000000..0ea7fdbb1e6
--- /dev/null
+++ b/test/lisp/color-tests.el
@@ -0,0 +1,251 @@
1;;; color-tests.el --- Tests for color.el -*- lexical-binding: t; -*-
2
3;; Copyright (C) 2017 Free Software Foundation, Inc.
4
5;; Author: Simen Heggestøyl <simenheg@gmail.com>
6;; Keywords:
7
8;; This file is part of GNU Emacs.
9
10;; GNU Emacs is free software: you can redistribute it and/or modify
11;; it under the terms of the GNU General Public License as published by
12;; the Free Software Foundation, either version 3 of the License, or
13;; (at your option) any later version.
14
15;; GNU Emacs is distributed in the hope that it will be useful,
16;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18;; GNU General Public License for more details.
19
20;; You should have received a copy of the GNU General Public License
21;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
22
23;;; Commentary:
24
25;;
26
27;;; Code:
28
29(require 'cl-lib)
30(require 'color)
31(require 'ert)
32(require 'seq)
33
34(defun color-tests--approx-equal (color1 color2)
35 "Return t if COLOR1 and COLOR2 are approximately equal."
36 (seq-every-p
37 (lambda (x) (< (abs x) 0.00001))
38 (cl-mapcar #'- color1 color2)))
39
40(ert-deftest color-tests-name-to-rgb ()
41 (should (equal (color-name-to-rgb "black") '(0.0 0.0 0.0)))
42 (should (equal (color-name-to-rgb "white") '(1.0 1.0 1.0)))
43 (should (equal (color-name-to-rgb "red") '(1.0 0.0 0.0)))
44 (should (equal (color-name-to-rgb "green") '(0.0 1.0 0.0)))
45 (should (equal (color-name-to-rgb "blue") '(0.0 0.0 1.0)))
46 (should (equal (color-name-to-rgb "#000000000000") '(0.0 0.0 0.0)))
47 (should (equal (color-name-to-rgb "#ffffffffffff") '(1.0 1.0 1.0)))
48 (should (equal (color-name-to-rgb "#ffff00000000") '(1.0 0.0 0.0)))
49 (should (equal (color-name-to-rgb "#0000ffff0000") '(0.0 1.0 0.0)))
50 (should (equal (color-name-to-rgb "#00000000ffff") '(0.0 0.0 1.0))))
51
52(ert-deftest color-tests-rgb-to-hex ()
53 (should (equal (color-rgb-to-hex 0 0 0) "#000000000000"))
54 (should (equal (color-rgb-to-hex 0 0 0 2) "#000000"))
55 (should (equal (color-rgb-to-hex 1 0 0) "#ffff00000000"))
56 (should (equal (color-rgb-to-hex 1 0 0 2) "#ff0000"))
57 (should (equal (color-rgb-to-hex 0.1 0.2 0.3) "#199933334ccc"))
58 (should (equal (color-rgb-to-hex 0.1 0.2 0.3 2) "#19334c")))
59
60(ert-deftest color-tests-complement ()
61 (should (equal (color-complement "white") '(0.0 0.0 0.0)))
62 (should (equal (color-complement "#ffffffffffff") '(0.0 0.0 0.0)))
63 (should (equal (color-complement "red") '(0.0 1.0 1.0))))
64
65(ert-deftest color-tests-gradient ()
66 (should-not (color-gradient '(0 0 0) '(255 255 255) 0))
67 (should
68 (equal (color-gradient '(0 0 0) '(255 255 255) 1)
69 '((127.5 127.5 127.5))))
70 (should
71 (equal (color-gradient '(0 0 0) '(255 255 255) 2)
72 '((85.0 85.0 85.0) (170.0 170.0 170.0))))
73 (should
74 (equal
75 (color-gradient '(255 192 203) '(250 128 114) 3)
76 '((253.75 176.0 180.75) (252.5 160.0 158.5) (251.25 144.0 136.25)))))
77
78(ert-deftest color-tests-hsl-to-rgb ()
79 (should (equal (color-hsl-to-rgb 0 0 0) '(0 0 0)))
80 (should (equal (color-hsl-to-rgb 360 0.5 0.5) '(0.75 0.25 0.25)))
81 (should (equal (color-hsl-to-rgb 123 0.2 0.9) '(0.92 0.88 0.88))))
82
83(ert-deftest color-tests-complement-hex ()
84 (should
85 (equal (color-complement-hex "#000000000000") "#ffffffffffff"))
86 (should
87 (equal (color-complement-hex "#ffff00000000") "#0000ffffffff")))
88
89(ert-deftest color-tests-rgb-to-hsv ()
90 (should (equal (color-rgb-to-hsv 0 0 0) '(0.0 0.0 0.0)))
91 (should (equal (color-rgb-to-hsv 1 1 1) '(0.0 0.0 1.0)))
92 (should (equal (color-rgb-to-hsv 1 0 0) '(0.0 1.0 1.0)))
93 (should (equal (color-rgb-to-hsv 0.5 0.3 0.3) '(0.0 0.4 0.5))))
94
95(ert-deftest color-tests-rgb-to-hsl ()
96 (should (equal (color-rgb-to-hsl 0 0 0) '(0.0 0.0 0.0)))
97 (should (equal (color-rgb-to-hsl 1 1 1) '(0.0 0.0 1.0)))
98 (should (equal (color-rgb-to-hsl 1 0 0) '(0.0 1 0.5)))
99 (should (equal (color-rgb-to-hsl 0.5 0.3 0.3) '(0.0 0.25 0.4))))
100
101(ert-deftest color-tests-srgb-to-xyz ()
102 (should (equal (color-srgb-to-xyz 0 0 0) '(0.0 0.0 0.0)))
103 (should
104 (equal (color-srgb-to-xyz 0 0 1) '(0.1804375 0.072175 0.9503041)))
105 (should
106 (color-tests--approx-equal
107 (color-srgb-to-xyz 0.1 0.2 0.3) '(0.0291865 0.031092 0.073738))))
108
109(ert-deftest color-tests-xyz-to-srgb ()
110 (should (equal (color-xyz-to-srgb 0 0 0) '(0.0 0.0 0.0)))
111 (should
112 (color-tests--approx-equal
113 (color-xyz-to-srgb 0.1804375 0.072175 0.9503041) '(0 0 1)))
114 (should
115 (color-tests--approx-equal
116 (color-xyz-to-srgb 0.0291865 0.031092 0.073738) '(0.1 0.2 0.3))))
117
118(ert-deftest color-tests-xyz-to-lab ()
119 (should (equal (color-xyz-to-lab 0 0 0) '(0.0 0.0 0.0)))
120 (should
121 (color-tests--approx-equal
122 (color-xyz-to-lab 0.1804375 0.072175 0.9503041)
123 '(32.2970109 79.1890315 -107.8646674)))
124 (should
125 (color-tests--approx-equal
126 (color-xyz-to-lab 0.1804375 0.072175 0.9503041 '(1 1 1))
127 '(32.2970109 74.3625763 -113.3597823)))
128 (should
129 (color-tests--approx-equal
130 (color-xyz-to-lab 0.0291865 0.031092 0.073738)
131 '(20.4760281 -0.6500752 -18.6340169))))
132
133(ert-deftest color-tests-lab-to-xyz ()
134 (should (equal (color-lab-to-xyz 0 0 0) '(0.0 0.0 0.0)))
135 (should
136 (color-tests--approx-equal
137 (color-lab-to-xyz 32.2970109 79.1890315 -107.8646674)
138 '(0.1804375 0.072175 0.9503041)))
139 (should
140 (color-tests--approx-equal
141 (color-lab-to-xyz 32.2970109 74.3625763 -113.3597823 '(1 1 1))
142 '(0.1804375 0.072175 0.9503041)))
143 (should
144 (color-tests--approx-equal
145 (color-lab-to-xyz 20.4760281 -0.6500752 -18.6340169)
146 '(0.0291865 0.031092 0.073738))))
147
148(ert-deftest color-tests-srgb-to-lab ()
149 (should (equal (color-srgb-to-lab 0 0 0) '(0.0 0.0 0.0)))
150 (should
151 (color-tests--approx-equal
152 (color-srgb-to-lab 0 1 0) '(87.7347223 -86.1808176 83.1770651)))
153 (should
154 (color-tests--approx-equal
155 (color-srgb-to-lab 0.1 0.2 0.3)
156 '(20.4762218 -0.6508996 -18.6340085))))
157
158(ert-deftest color-tests-lab-to-srgb ()
159 (should (equal (color-lab-to-srgb 0 0 0) '(0.0 0.0 0.0)))
160 (should
161 (color-tests--approx-equal
162 (color-lab-to-srgb 87.7347223 -86.1808176 83.1770651) '(0 1 0)))
163 (should
164 (color-tests--approx-equal
165 (color-lab-to-srgb 20.4762218 -0.6508996 -18.6340085)
166 '(0.1 0.2 0.3))))
167
168(ert-deftest color-tests-cie-de2000 ()
169 (should (= (color-cie-de2000 '(0 0 0) '(0 0 0)) 0.0))
170 (should
171 (color-tests--approx-equal
172 (list
173 (color-cie-de2000
174 (color-srgb-to-lab 1 0 0) (color-srgb-to-lab 0 0 1)))
175 '(52.8803934)))
176 (should
177 (color-tests--approx-equal
178 (list
179 (color-cie-de2000
180 (color-srgb-to-lab 0.8 0 0) (color-srgb-to-lab 0.9 0 0)))
181 '(5.3844503))))
182
183(ert-deftest color-tests-clamp ()
184 (should (= (color-clamp 0) 0.0))
185 (should (= (color-clamp -1) 0.0))
186 (should (= (color-clamp 0.5) 0.5))
187 (should (= (color-clamp 1) 1.0))
188 (should (= (color-clamp 1.1) 1.0)))
189
190(ert-deftest color-tests-saturate-hsl ()
191 (should (equal (color-saturate-hsl 360 0.5 0.5 0) '(360 0.5 0.5)))
192 (should (equal (color-saturate-hsl 360 0.5 0.5 -10) '(360 0.4 0.5)))
193 (should
194 (equal (color-saturate-hsl 360 0.5 0.5 -500) '(360 0.0 0.5)))
195 (should (equal (color-saturate-hsl 120 0.5 0.8 5) '(120 0.55 0.8)))
196 (should
197 (equal (color-saturate-hsl 120 0.5 0.8 500) '(120 1.0 0.8))))
198
199(ert-deftest color-tests-saturate-name ()
200 (should (equal (color-saturate-name "black" 100) "#000000000000"))
201 (should (equal (color-saturate-name "white" 100) "#ffffffffffff"))
202 (should (equal (color-saturate-name "red" 0) "#ffff00000000"))
203 (should (equal (color-saturate-name "red" 50) "#ffff00000000")))
204
205(ert-deftest color-tests-desaturate-hsl ()
206 (should (equal (color-desaturate-hsl 360 0.5 0.5 0) '(360 0.5 0.5)))
207 (should
208 (equal (color-desaturate-hsl 360 0.5 0.5 -10) '(360 0.6 0.5)))
209 (should
210 (equal (color-desaturate-hsl 360 0.5 0.5 -500) '(360 1.0 0.5)))
211 (should
212 (equal (color-desaturate-hsl 120 0.5 0.8 5) '(120 0.45 0.8)))
213 (should
214 (equal (color-desaturate-hsl 120 0.5 0.8 500) '(120 0.0 0.8))))
215
216(ert-deftest color-tests-desaturate-name ()
217 (should (equal (color-desaturate-name "black" 100) "#000000000000"))
218 (should (equal (color-desaturate-name "white" 100) "#ffffffffffff"))
219 (should (equal (color-desaturate-name "red" 0) "#ffff00000000")))
220
221(ert-deftest color-tests-lighten-hsl ()
222 (should (equal (color-lighten-hsl 360 0.5 0.5 0) '(360 0.5 0.5)))
223 (should (equal (color-lighten-hsl 360 0.5 0.5 -10) '(360 0.5 0.4)))
224 (should (equal (color-lighten-hsl 360 0.5 0.5 -500) '(360 0.5 0.0)))
225 (should
226 (color-tests--approx-equal
227 (color-lighten-hsl 120 0.5 0.8 5) '(120 0.5 0.85)))
228 (should
229 (equal (color-lighten-hsl 120 0.5 0.8 500) '(120 0.5 1.0))))
230
231(ert-deftest color-tests-lighten-name ()
232 (should (equal (color-lighten-name "black" 100) "#ffffffffffff"))
233 (should (equal (color-lighten-name "white" 100) "#ffffffffffff"))
234 (should (equal (color-lighten-name "red" 0) "#ffff00000000"))
235 (should (equal (color-lighten-name "red" 10) "#ffff33323332")))
236
237(ert-deftest color-tests-darken-hsl ()
238 (should (equal (color-darken-hsl 360 0.5 0.5 0) '(360 0.5 0.5)))
239 (should (equal (color-darken-hsl 360 0.5 0.5 -10) '(360 0.5 0.6)))
240 (should (equal (color-darken-hsl 360 0.5 0.5 -500) '(360 0.5 1.0)))
241 (should (equal (color-darken-hsl 120 0.5 0.8 5) '(120 0.5 0.75)))
242 (should (equal (color-darken-hsl 120 0.5 0.8 500) '(120 0.5 0.0))))
243
244(ert-deftest color-tests-darken-name ()
245 (should (equal (color-darken-name "black" 100) "#000000000000"))
246 (should (equal (color-darken-name "white" 100) "#000000000000"))
247 (should (equal (color-darken-name "red" 0) "#ffff00000000"))
248 (should (equal (color-darken-name "red" 10) "#cccc00000000")))
249
250(provide 'color-tests)
251;;; color-tests.el ends here