summaryrefslogtreecommitdiff
path: root/packages/taler-wallet-webextension/src/mui/colors/manipulation.test.ts
blob: 78e9d9cf74ce9776b9f098950c03556481d2a84b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/*
 This file is part of GNU Taler
 (C) 2022 Taler Systems S.A.

 GNU Taler is free software; you can redistribute it and/or modify it under the
 terms of the GNU General Public License as published by the Free Software
 Foundation; either version 3, or (at your option) any later version.

 GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY
 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 A PARTICULAR PURPOSE.  See the GNU General Public License for more details.

 You should have received a copy of the GNU General Public License along with
 GNU Taler; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
 */
import { expect } from "chai";
import {
  recomposeColor,
  hexToRgb,
  rgbToHex,
  hslToRgb,
  darken,
  decomposeColor,
  emphasize,
  alpha,
  getContrastRatio,
  getLuminance,
  lighten,
} from "./manipulation.js";

describe("utils/colorManipulator", () => {
  describe("recomposeColor", () => {
    it("converts a decomposed rgb color object to a string` ", () => {
      expect(
        recomposeColor({
          type: "rgb",
          values: [255, 255, 255],
        }),
      ).to.equal("rgb(255, 255, 255)");
    });

    it("converts a decomposed rgba color object to a string` ", () => {
      expect(
        recomposeColor({
          type: "rgba",
          values: [255, 255, 255, 0.5],
        }),
      ).to.equal("rgba(255, 255, 255, 0.5)");
    });

    it("converts a decomposed hsl color object to a string` ", () => {
      expect(
        recomposeColor({
          type: "hsl",
          values: [100, 50, 25],
        }),
      ).to.equal("hsl(100, 50%, 25%)");
    });

    it("converts a decomposed hsla color object to a string` ", () => {
      expect(
        recomposeColor({
          type: "hsla",
          values: [100, 50, 25, 0.5],
        }),
      ).to.equal("hsla(100, 50%, 25%, 0.5)");
    });
  });

  describe("hexToRgb", () => {
    it("converts a short hex color to an rgb color` ", () => {
      expect(hexToRgb("#9f3")).to.equal("rgb(153, 255, 51)");
    });

    it("converts a long hex color to an rgb color` ", () => {
      expect(hexToRgb("#a94fd3")).to.equal("rgb(169, 79, 211)");
    });

    it("converts a long alpha hex color to an argb color` ", () => {
      expect(hexToRgb("#111111f8")).to.equal("rgba(17, 17, 17, 0.973)");
    });
  });

  describe("rgbToHex", () => {
    it("converts an rgb color to a hex color` ", () => {
      expect(rgbToHex("rgb(169, 79, 211)")).to.equal("#a94fd3");
    });

    it("converts an rgba color to a hex color` ", () => {
      expect(rgbToHex("rgba(169, 79, 211, 1)")).to.equal("#a94fd3ff");
    });

    it("idempotent", () => {
      expect(rgbToHex("#A94FD3")).to.equal("#A94FD3");
    });
  });

  describe("hslToRgb", () => {
    it("converts an hsl color to an rgb color` ", () => {
      expect(hslToRgb("hsl(281, 60%, 57%)")).to.equal("rgb(169, 80, 211)");
    });

    it("converts an hsla color to an rgba color` ", () => {
      expect(hslToRgb("hsla(281, 60%, 57%, 0.5)")).to.equal(
        "rgba(169, 80, 211, 0.5)",
      );
    });

    it("allow to convert values only", () => {
      expect(hslToRgb("hsl(281, 60%, 57%)")).to.equal("rgb(169, 80, 211)");
    });
  });

  describe("decomposeColor", () => {
    it("converts an rgb color string to an object with `type` and `value` keys", () => {
      const { type, values } = decomposeColor("rgb(255, 255, 255)");
      expect(type).to.equal("rgb");
      expect(values).to.deep.equal([255, 255, 255]);
    });

    it("converts an rgba color string to an object with `type` and `value` keys", () => {
      const { type, values } = decomposeColor("rgba(255, 255, 255, 0.5)");
      expect(type).to.equal("rgba");
      expect(values).to.deep.equal([255, 255, 255, 0.5]);
    });

    it("converts an hsl color string to an object with `type` and `value` keys", () => {
      const { type, values } = decomposeColor("hsl(100, 50%, 25%)");
      expect(type).to.equal("hsl");
      expect(values).to.deep.equal([100, 50, 25]);
    });

    it("converts an hsla color string to an object with `type` and `value` keys", () => {
      const { type, values } = decomposeColor("hsla(100, 50%, 25%, 0.5)");
      expect(type).to.equal("hsla");
      expect(values).to.deep.equal([100, 50, 25, 0.5]);
    });

    it("converts rgba hex", () => {
      const decomposed = decomposeColor("#111111f8");
      expect(decomposed).to.deep.equal({
        type: "rgba",
        colorSpace: undefined,
        values: [17, 17, 17, 0.973],
      });
    });
  });

  describe("getContrastRatio", () => {
    it("returns a ratio for black : white", () => {
      expect(getContrastRatio("#000", "#FFF")).to.equal(21);
    });

    it("returns a ratio for black : black", () => {
      expect(getContrastRatio("#000", "#000")).to.equal(1);
    });

    it("returns a ratio for white : white", () => {
      expect(getContrastRatio("#FFF", "#FFF")).to.equal(1);
    });

    it("returns a ratio for dark-grey : light-grey", () => {
      expect(getContrastRatio("#707070", "#E5E5E5")).to.be.approximately(
        3.93,
        0.01,
      );
    });

    it("returns a ratio for black : light-grey", () => {
      expect(getContrastRatio("#000", "#888")).to.be.approximately(5.92, 0.01);
    });
  });

  describe("getLuminance", () => {
    it("returns a valid luminance for rgb white ", () => {
      expect(getLuminance("rgba(255, 255, 255)")).to.equal(1);
      expect(getLuminance("rgb(255, 255, 255)")).to.equal(1);
    });

    it("returns a valid luminance for rgb mid-grey", () => {
      expect(getLuminance("rgba(127, 127, 127)")).to.equal(0.212);
      expect(getLuminance("rgb(127, 127, 127)")).to.equal(0.212);
    });

    it("returns a valid luminance for an rgb color", () => {
      expect(getLuminance("rgb(255, 127, 0)")).to.equal(0.364);
    });

    it("returns a valid luminance from an hsl color", () => {
      expect(getLuminance("hsl(100, 100%, 50%)")).to.equal(0.735);
    });

    it("returns an equal luminance for the same color in different formats", () => {
      const hsl = "hsl(100, 100%, 50%)";
      const rgb = "rgb(85, 255, 0)";
      expect(getLuminance(hsl)).to.equal(getLuminance(rgb));
    });
  });

  describe("emphasize", () => {
    it("lightens a dark rgb color with the coefficient provided", () => {
      expect(emphasize("rgb(1, 2, 3)", 0.4)).to.equal(
        lighten("rgb(1, 2, 3)", 0.4),
      );
    });

    it("darkens a light rgb color with the coefficient provided", () => {
      expect(emphasize("rgb(250, 240, 230)", 0.3)).to.equal(
        darken("rgb(250, 240, 230)", 0.3),
      );
    });

    it("lightens a dark rgb color with the coefficient 0.15 by default", () => {
      expect(emphasize("rgb(1, 2, 3)")).to.equal(lighten("rgb(1, 2, 3)", 0.15));
    });

    it("darkens a light rgb color with the coefficient 0.15 by default", () => {
      expect(emphasize("rgb(250, 240, 230)")).to.equal(
        darken("rgb(250, 240, 230)", 0.15),
      );
    });
  });

  describe("alpha", () => {
    it("converts an rgb color to an rgba color with the value provided", () => {
      expect(alpha("rgb(1, 2, 3)", 0.4)).to.equal("rgba(1, 2, 3, 0.4)");
    });

    it("updates an rgba color with the alpha value provided", () => {
      expect(alpha("rgba(255, 0, 0, 0.2)", 0.5)).to.equal(
        "rgba(255, 0, 0, 0.5)",
      );
    });

    it("converts an hsl color to an hsla color with the value provided", () => {
      expect(alpha("hsl(0, 100%, 50%)", 0.1)).to.equal(
        "hsla(0, 100%, 50%, 0.1)",
      );
    });

    it("updates an hsla color with the alpha value provided", () => {
      expect(alpha("hsla(0, 100%, 50%, 0.2)", 0.5)).to.equal(
        "hsla(0, 100%, 50%, 0.5)",
      );
    });
  });

  describe("darken", () => {
    it("doesn't modify rgb black", () => {
      expect(darken("rgb(0, 0, 0)", 0.1)).to.equal("rgb(0, 0, 0)");
    });

    it("darkens rgb white to black when coefficient is 1", () => {
      expect(darken("rgb(255, 255, 255)", 1)).to.equal("rgb(0, 0, 0)");
    });

    it("retains the alpha value in an rgba color", () => {
      expect(darken("rgba(0, 0, 0, 0.5)", 0.1)).to.equal("rgba(0, 0, 0, 0.5)");
    });

    it("darkens rgb white by 10% when coefficient is 0.1", () => {
      expect(darken("rgb(255, 255, 255)", 0.1)).to.equal("rgb(229, 229, 229)");
    });

    it("darkens rgb red by 50% when coefficient is 0.5", () => {
      expect(darken("rgb(255, 0, 0)", 0.5)).to.equal("rgb(127, 0, 0)");
    });

    it("darkens rgb grey by 50% when coefficient is 0.5", () => {
      expect(darken("rgb(127, 127, 127)", 0.5)).to.equal("rgb(63, 63, 63)");
    });

    it("doesn't modify rgb colors when coefficient is 0", () => {
      expect(darken("rgb(255, 255, 255)", 0)).to.equal("rgb(255, 255, 255)");
    });

    it("darkens hsl red by 50% when coefficient is 0.5", () => {
      expect(darken("hsl(0, 100%, 50%)", 0.5)).to.equal("hsl(0, 100%, 25%)");
    });

    it("doesn't modify hsl colors when coefficient is 0", () => {
      expect(darken("hsl(0, 100%, 50%)", 0)).to.equal("hsl(0, 100%, 50%)");
    });

    it("doesn't modify hsl colors when l is 0%", () => {
      expect(darken("hsl(0, 50%, 0%)", 0.5)).to.equal("hsl(0, 50%, 0%)");
    });
  });

  describe("lighten", () => {
    it("doesn't modify rgb white", () => {
      expect(lighten("rgb(255, 255, 255)", 0.1)).to.equal("rgb(255, 255, 255)");
    });

    it("lightens rgb black to white when coefficient is 1", () => {
      expect(lighten("rgb(0, 0, 0)", 1)).to.equal("rgb(255, 255, 255)");
    });

    it("retains the alpha value in an rgba color", () => {
      expect(lighten("rgba(255, 255, 255, 0.5)", 0.1)).to.equal(
        "rgba(255, 255, 255, 0.5)",
      );
    });

    it("lightens rgb black by 10% when coefficient is 0.1", () => {
      expect(lighten("rgb(0, 0, 0)", 0.1)).to.equal("rgb(25, 25, 25)");
    });

    it("lightens rgb red by 50% when coefficient is 0.5", () => {
      expect(lighten("rgb(255, 0, 0)", 0.5)).to.equal("rgb(255, 127, 127)");
    });

    it("lightens rgb grey by 50% when coefficient is 0.5", () => {
      expect(lighten("rgb(127, 127, 127)", 0.5)).to.equal("rgb(191, 191, 191)");
    });

    it("doesn't modify rgb colors when coefficient is 0", () => {
      expect(lighten("rgb(127, 127, 127)", 0)).to.equal("rgb(127, 127, 127)");
    });

    it("lightens hsl red by 50% when coefficient is 0.5", () => {
      expect(lighten("hsl(0, 100%, 50%)", 0.5)).to.equal("hsl(0, 100%, 75%)");
    });

    it("doesn't modify hsl colors when coefficient is 0", () => {
      expect(lighten("hsl(0, 100%, 50%)", 0)).to.equal("hsl(0, 100%, 50%)");
    });

    it("doesn't modify hsl colors when `l` is 100%", () => {
      expect(lighten("hsl(0, 50%, 100%)", 0.5)).to.equal("hsl(0, 50%, 100%)");
    });
  });
});