Skip to content

Commit 2762eb2

Browse files
authored
Add full tests for Color and Swatch classes (#259)
1 parent 02781e5 commit 2762eb2

File tree

3 files changed

+102
-1
lines changed

3 files changed

+102
-1
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"type": "patch",
3+
"comment": "Add full tests for Color and Swatch classes",
4+
"packageName": "@adaptive-web/adaptive-ui",
5+
"email": "47367562+bheston@users.noreply.github.com",
6+
"dependentChangeType": "patch"
7+
}

packages/adaptive-ui/src/core/color/color.spec.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import chai from "chai";
22
import { Color } from "./color.js";
33
import { type Rgb } from "culori/fn";
4+
import { _white } from "./utilities/color-constants.js";
45

56
const { expect } = chai;
67

@@ -43,4 +44,28 @@ describe("Color", () => {
4344
expect(color.color).to.deep.equal(opacityColor);
4445
expect(color.toColorString()).to.equal(opacityRgba);
4546
});
47+
48+
it("should provide the correct relative luminance", () => {
49+
const color = Color.from(greyObject);
50+
51+
expect(color.relativeLuminance).to.approximately(0.21, 0.01);
52+
});
53+
54+
it("should provide a string representation", () => {
55+
const color = Color.from(greyObject);
56+
57+
expect(color.toString()).to.equal(greyHex);
58+
});
59+
60+
it("should provide a css representation", () => {
61+
const color = Color.from(greyObject);
62+
63+
expect(color.createCSS()).to.equal(greyHex);
64+
});
65+
66+
it("should provide its contrast with another value", () => {
67+
const color = Color.from(greyObject);
68+
69+
expect(color.contrast(_white)).to.approximately(3.9, 0.1);
70+
});
4671
});

packages/adaptive-ui/src/core/color/swatch.spec.ts

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import chai from "chai";
2+
import { Color } from "./color.js";
23
import { Swatch } from "./swatch.js";
34
import { type Rgb } from "culori/fn";
5+
import { _white } from "./utilities/color-constants.js";
46

57
const { expect } = chai;
68

@@ -12,6 +14,9 @@ const opacityColor: Rgb = { mode: "rgb", r: 0.2, g: 0.4, b: 0.6, alpha: 0.501960
1214
const opacityHex = "#33669980";
1315
const opacityRgba = "rgba(51, 102, 153, 0.5)";
1416

17+
const lightGreyObject = { r: 0.75, g: 0.75, b: 0.75, alpha: undefined };
18+
const darkGreyObject = { r: 0.25, g: 0.25, b: 0.25, alpha: undefined };
19+
1520
describe("Swatch", () => {
1621
it("should create a Swatch from the provided object", () => {
1722
const swatch = Swatch.from(greyObject);
@@ -29,7 +34,16 @@ describe("Swatch", () => {
2934
expect(swatch.toColorString()).to.equal(greyHex);
3035
});
3136

32-
it("should create a Swatch from the provided hex swatch", () => {
37+
it("should create a Swatch from a Color", () => {
38+
const color = Color.from(greyObject);
39+
const swatch = Swatch.fromColor(color);
40+
41+
expect(swatch).to.be.instanceof(Swatch);
42+
expect(swatch.color).to.deep.equal(greyColor);
43+
expect(swatch.toColorString()).to.equal(greyHex);
44+
});
45+
46+
it("should create a Swatch from the provided hex color", () => {
3347
const swatch = Swatch.parse(greyHex)!;
3448

3549
expect(swatch).to.be.instanceof(Swatch);
@@ -43,4 +57,59 @@ describe("Swatch", () => {
4357
expect(swatch.color).to.deep.equal(opacityColor);
4458
expect(swatch.toColorString()).to.equal(opacityRgba);
4559
});
60+
61+
it("should create a light Swatch as an overlay", () => {
62+
const lightGrey = Swatch.from(lightGreyObject);
63+
const grey = Swatch.from(greyObject);
64+
const swatch = Swatch.asOverlay(lightGrey, grey)!;
65+
66+
expect(swatch).to.be.instanceof(Swatch);
67+
expect(swatch.color).to.deep.equal({ mode: "rgb", r: 1, g: 1, b: 1, alpha: 0.5 });
68+
});
69+
70+
it("should create a dark Swatch as an overlay", () => {
71+
const darkGrey = Swatch.from(darkGreyObject);
72+
const grey = Swatch.from(greyObject);
73+
const swatch = Swatch.asOverlay(darkGrey, grey)!;
74+
75+
expect(swatch).to.be.instanceof(Swatch);
76+
expect(swatch.color).to.deep.equal({ mode: "rgb", r: 0, g: 0, b: 0, alpha: 0.5 });
77+
});
78+
79+
it("should provide the correct relative luminance", () => {
80+
const swatch = Swatch.from(greyObject);
81+
82+
expect(swatch.relativeLuminance).to.approximately(0.21, 0.01);
83+
});
84+
85+
it("should provide a string representation", () => {
86+
const swatch = Swatch.from(greyObject);
87+
88+
expect(swatch.toString()).to.equal(greyHex);
89+
});
90+
91+
it("should provide a css representation", () => {
92+
const swatch = Swatch.from(greyObject);
93+
94+
expect(swatch.createCSS()).to.equal(greyHex);
95+
});
96+
97+
it("should provide its contrast with another value", () => {
98+
const swatch = Swatch.from(greyObject);
99+
100+
expect(swatch.contrast(_white)).to.approximately(3.9, 0.1);
101+
});
102+
103+
it("should provide a transparent equivalent", () => {
104+
const swatch = Swatch.from(greyObject);
105+
106+
const halfTransparent = swatch.toTransparent(0.5);
107+
expect(halfTransparent).to.be.instanceof(Swatch);
108+
expect(halfTransparent.color).to.deep.equal(Object.assign(greyColor, { alpha: 0.5 }));
109+
expect(halfTransparent.toColorString()).to.equal("rgba(128, 128, 128, 0.5)");
110+
expect(halfTransparent.toString()).to.equal("rgba(128, 128, 128, 0.5)");
111+
expect(halfTransparent.createCSS()).to.equal("rgba(128, 128, 128, 0.5)");
112+
expect(halfTransparent.relativeLuminance).to.approximately(0.21, 0.01);
113+
expect(halfTransparent.contrast(_white)).to.approximately(3.9, 0.1);
114+
});
46115
});

0 commit comments

Comments
 (0)