Skip to content

Commit

Permalink
Revamp coord reading/writing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
LeaVerou committed May 22, 2024
1 parent d49b1ae commit d699db0
Show file tree
Hide file tree
Showing 2 changed files with 163 additions and 97 deletions.
163 changes: 163 additions & 0 deletions test/coords.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
import Color from "../src/index.js";
import lch from "../src/spaces/lch.js";
import { check } from "./util.mjs";

export default {
name: "Coordinate reading / writing tests",
description: "These tests modify one or more coordinates and check the result.",
check: check.deep(check.proximity({epsilon: .005})),
data: {
red: new Color("srgb", [1, 0, 0]),
red_50: new Color("srgb", [1, 0, 0], .5),
red_oklch: new Color("oklch", [.6, .25, 30]),
},
tests: [
{
name: "Reading coordinates",
tests: [
{
name: "color.coords",
run() {

Check warning on line 20 in test/coords.js

View workflow job for this annotation

GitHub Actions / Lint & Test Types

Missing space before function parentheses
return this.data.red.coords;
},
expect: [1, 0, 0],
},
{
name: "color.getAll()",
run() {

Check warning on line 27 in test/coords.js

View workflow job for this annotation

GitHub Actions / Lint & Test Types

Missing space before function parentheses
return this.data.red.getAll();
},
expect: [1, 0, 0],
},
{
name: "color.alpha",
run() {

Check warning on line 34 in test/coords.js

View workflow job for this annotation

GitHub Actions / Lint & Test Types

Missing space before function parentheses
return this.data.red_50.alpha;
},
expect: 0.5,
},
{
name: "color.coords[1]",
run() {
return this.data.red.coords[1];
},
expect: 0,
},
{
name: "color.coordId",
run() {
return this.data.red_oklch.h;
},
expect: 30,
},
{
name: "color.get(coordId)",
run() {
return this.data.red_oklch.get("h");
},
expect: 30,
},
{
name: "color.otherSpace.coordId",
run() {
return this.data.red.oklch.h;
},
expect: 29.23,
},
{
name: "color.get(otherSpace.coordId)",
run() {
return this.data.red.get("oklch.hue");
},
expect: 29.23,
},
]
},
{
name: "Writing coordinates",
tests: [
{
name: "color.coords = newCoords",
run () {
let color = new Color("srgb", [0, 1, 0]);
color.coords = [1, 0, 1];
return color.coords[2];
},
expect: 1,
},
{
name: "color.coords[index] = value",
run () {
let color = this.data.red.clone();
color.coords[1] = 0.5;
return color.coords[1];
},
expect: 0.5,
},
{
name: "color.alpha = value",
run () {
let color = this.data.red.clone();
color.alpha = 0.5;
return color.alpha;
},
expect: 0.5,
},
{
name: "color.space.coordId = value",
run () {
let color = this.data.red.clone();
color.lch.c = 13;
return color.get("lch.c");
},
expect: 13,
},
{
name: "color.set(coordId, value)",
run () {
let color = this.data.red.to("lch");
color.set("c", 13);
return color.lch.c;
},
expect: 13,
},
{
name: "color.set(\"otherSpace.coordId\", value)",
run () {
let color = this.data.red.clone();
color.set("lch.c", 13);
return color.get("lch.c");
},
expect: 13,
},
{
name: "color.space[index] = value",
run () {
let color = this.data.red.clone();
color.lch[1] = 13;
return color.lch.c;
},
expect: 13,
},
{
name: "color.set(coordId, fn)",
run () {
let color = new Color("slategray").to("lch");
color.set("c", c => c * 1.2);
return color.lch.c;
},
expect: 13.480970445148008,
},
{
name: "color.set(coordsObject)",
run () {
let color = this.data.red.clone();
color.set({"lch.c": 13, "lch.l": 40});
return [color.lch.c, color.lch.l];
},
expect: [13, 40],
},
],
},
],
};
97 changes: 0 additions & 97 deletions test/modifications.js

This file was deleted.

0 comments on commit d699db0

Please sign in to comment.