Skip to content

Commit

Permalink
Tests for harmonies, default to hsv format
Browse files Browse the repository at this point in the history
  • Loading branch information
mster committed May 17, 2021
1 parent b71ea0f commit 98887d3
Show file tree
Hide file tree
Showing 2 changed files with 343 additions and 7 deletions.
14 changes: 7 additions & 7 deletions lib/harmonies.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ module.exports = {
const SUPPORTED_SPACES = require("./spaces");
const { shiftDegrees, xspace, cspace } = require("./utils");

function complementary(color, format) {
function complementary(color, format = "hsv") {
format = format.toLowerCase();

const { h, s, v, ...rest } = preValidation(color, format);
Expand All @@ -26,7 +26,7 @@ function complementary(color, format) {
return postFormat(harmony, format);
}

function splitComplementary(color, format) {
function splitComplementary(color, format = "hsv") {
format = format.toLowerCase();

const { h, s, v, ...rest } = preValidation(color, format);
Expand All @@ -40,7 +40,7 @@ function splitComplementary(color, format) {
return postFormat(harmony, format);
}

function triadic(color, format) {
function triadic(color, format = "hsv") {
format = format.toLowerCase();

const { h, s, v, ...rest } = preValidation(color, format);
Expand All @@ -54,7 +54,7 @@ function triadic(color, format) {
return postFormat(harmony, format);
}

function tetradic(color, format) {
function tetradic(color, format = "hsv") {
format = format.toLowerCase();

const { h, s, v, ...rest } = preValidation(color, format);
Expand All @@ -69,7 +69,7 @@ function tetradic(color, format) {
return postFormat(harmony, format);
}

function analagous(color, format) {
function analagous(color, format = "hsv") {
format = format.toLowerCase();

const { h, s, v, ...rest } = preValidation(color, format);
Expand All @@ -84,7 +84,7 @@ function analagous(color, format) {
return postFormat(harmony, format);
}

function stepSixty(color, format) {
function stepSixty(color, format = "hsv") {
format = format.toLowerCase();

const { h, s, v, ...rest } = preValidation(color, format);
Expand All @@ -101,7 +101,7 @@ function stepSixty(color, format) {
return postFormat(harmony, format);
}

function analagousTight(color, format) {
function analagousTight(color, format = "hsv") {
format = format.toLowerCase();

const { h, s, v, ...rest } = preValidation(color, format);
Expand Down
336 changes: 336 additions & 0 deletions tests/test.harmonies.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,336 @@
"use strict";

const { harmonies } = require("../index");

describe("Harmony", () => {
test("complementary should return an array of complentary colors, defaulting to HSV.", () => {
const { complementary } = harmonies;

const base = { r: 56, g: 45, b: 106 };

const expected = [
{ h: 251, s: 0.575, v: 0.416 },
{ h: 71, s: 0.575, v: 0.416 },
];
const actual = complementary(base);

expect(actual).toEqual(expected);
});

test("complementary should return an array of complentary colors, reformatting to the given color space.", () => {
const { complementary } = harmonies;

const base = { r: 56, g: 45, b: 106 };

const expected = [
{ r: 56, g: 45, b: 106 },
{ r: 95, g: 106, b: 45 },
];
const actual = complementary(base, "rgb");

expect(actual).toEqual(expected);
});

test("complementary should throw when passed invalid arguments.", () => {
const { complementary } = harmonies;

const badColor = null;
expect(() => {
complementary(badColor);
}).toThrow();

const goodColor = "#FFFFFF";
const badFormat = "CMYK";
expect(() => {
complementary(goodColor, badFormat);
}).toThrow();
});

test("splitComplementary should return an array of complentary colors, defaulting to HSV.", () => {
const { splitComplementary } = harmonies;

const base = { r: 56, g: 45, b: 106 };

const expected = [
{ h: 251, s: 0.575, v: 0.416 },
{ h: 41, s: 0.575, v: 0.416 },
{ h: 101, s: 0.575, v: 0.416 },
];
const actual = splitComplementary(base);

expect(actual).toEqual(expected);
});

test("splitComplementary should return an array of complentary colors, reformatting to the given color space.", () => {
const { splitComplementary } = harmonies;

const base = { r: 56, g: 45, b: 106 };

const expected = [
{ r: 56, g: 45, b: 106 },
{ r: 106, g: 87, b: 45 },
{ r: 64, g: 106, b: 45 },
];
const actual = splitComplementary(base, "rgb");

expect(actual).toEqual(expected);
});

test("splitComplementary should throw when passed invalid arguments.", () => {
const { splitComplementary } = harmonies;

const badColor = null;
expect(() => {
splitComplementary(badColor);
}).toThrow();

const goodColor = "#FFFFFF";
const badFormat = "CMYK";
expect(() => {
splitComplementary(goodColor, badFormat);
}).toThrow();
});

test("triadic should return an array of complentary colors, defaulting to HSV.", () => {
const { triadic } = harmonies;

const base = { r: 56, g: 45, b: 106 };

const expected = [
{ h: 251, s: 0.575, v: 0.416 },
{ h: 11, s: 0.575, v: 0.416 },
{ h: 131, s: 0.575, v: 0.416 },
];
const actual = triadic(base);

expect(actual).toEqual(expected);
});

test("triadic should return an array of complentary colors, reformatting to the given color space.", () => {
const { triadic } = harmonies;

const base = { r: 56, g: 45, b: 106 };

const expected = [
{ r: 56, g: 45, b: 106 },
{ r: 106, g: 56, b: 45 },
{ r: 45, g: 106, b: 56 },
];
const actual = triadic(base, "rgb");

expect(actual).toEqual(expected);
});

test("triadic should throw when passed invalid arguments.", () => {
const { triadic } = harmonies;

const badColor = null;
expect(() => {
triadic(badColor);
}).toThrow();

const goodColor = "#FFFFFF";
const badFormat = "CMYK";
expect(() => {
triadic(goodColor, badFormat);
}).toThrow();
});

test("tetradic should return an array of complentary colors, defaulting to HSV.", () => {
const { tetradic } = harmonies;

const base = { r: 56, g: 45, b: 106 };

const expected = [
{ h: 251, s: 0.575, v: 0.416 },
{ h: 341, s: 0.575, v: 0.416 },
{ h: 71, s: 0.575, v: 0.416 },
{ h: 161, s: 0.575, v: 0.416 },
];
const actual = tetradic(base);

expect(actual).toEqual(expected);
});

test("tetradic should return an array of complentary colors, reformatting to the given color space.", () => {
const { tetradic } = harmonies;

const base = { r: 56, g: 45, b: 106 };

const expected = [
{ r: 56, g: 45, b: 106 },
{ r: 106, g: 45, b: 64 },
{ r: 95, g: 106, b: 45 },
{ r: 45, g: 106, b: 87 },
];
const actual = tetradic(base, "rgb");

expect(actual).toEqual(expected);
});

test("tetradic should throw when passed invalid arguments.", () => {
const { tetradic } = harmonies;

const badColor = null;
expect(() => {
tetradic(badColor);
}).toThrow();

const goodColor = "#FFFFFF";
const badFormat = "CMYK";
expect(() => {
tetradic(goodColor, badFormat);
}).toThrow();
});

test("analagous should return an array of complentary colors, defaulting to HSV.", () => {
const { analagous } = harmonies;

const base = { r: 56, g: 45, b: 106 };

const expected = [
{ h: 251, s: 0.575, v: 0.416 },
{ h: 281, s: 0.575, v: 0.416 },
{ h: 311, s: 0.575, v: 0.416 },
{ h: 341, s: 0.575, v: 0.416 },
];
const actual = analagous(base);

expect(actual).toEqual(expected);
});

test("analagous should return an array of complentary colors, reformatting to the given color space.", () => {
const { analagous } = harmonies;

const base = { r: 56, g: 45, b: 106 };

const expected = [
{ r: 56, g: 45, b: 106 },
{ r: 87, g: 45, b: 106 },
{ r: 106, g: 45, b: 95 },
{ r: 106, g: 45, b: 64 },
];
const actual = analagous(base, "rgb");

expect(actual).toEqual(expected);
});

test("analagous should throw when passed invalid arguments.", () => {
const { analagous } = harmonies;

const badColor = null;
expect(() => {
analagous(badColor);
}).toThrow();

const goodColor = "#FFFFFF";
const badFormat = "CMYK";
expect(() => {
analagous(goodColor, badFormat);
}).toThrow();
});

test("stepSixty should return an array of complentary colors, defaulting to HSV.", () => {
const { stepSixty } = harmonies;

const base = { r: 56, g: 45, b: 106 };

const expected = [
{ h: 251, s: 0.575, v: 0.416 },
{ h: 311, s: 0.575, v: 0.416 },
{ h: 11, s: 0.575, v: 0.416 },
{ h: 71, s: 0.575, v: 0.416 },
{ h: 131, s: 0.575, v: 0.416 },
{ h: 191, s: 0.575, v: 0.416 },
];
const actual = stepSixty(base);

expect(actual).toEqual(expected);
});

test("stepSixty should return an array of complentary colors, reformatting to the given color space.", () => {
const { stepSixty } = harmonies;

const base = { r: 56, g: 45, b: 106 };

const expected = [
{ r: 56, g: 45, b: 106 },
{ r: 106, g: 45, b: 95 },
{ r: 106, g: 56, b: 45 },
{ r: 95, g: 106, b: 45 },
{ r: 45, g: 106, b: 56 },
{ r: 45, g: 95, b: 106 },
];
const actual = stepSixty(base, "rgb");

expect(actual).toEqual(expected);
});

test("stepSixty should throw when passed invalid arguments.", () => {
const { stepSixty } = harmonies;

const badColor = null;
expect(() => {
stepSixty(badColor);
}).toThrow();

const goodColor = "#FFFFFF";
const badFormat = "CMYK";
expect(() => {
stepSixty(goodColor, badFormat);
}).toThrow();
});

test("analagousTight should return an array of complentary colors, defaulting to HSV.", () => {
const { analagousTight } = harmonies;

const base = { r: 56, g: 45, b: 106 };

const expected = [
{ h: 251, s: 0.575, v: 0.416 },
{ h: 266, s: 0.575, v: 0.416 },
{ h: 281, s: 0.575, v: 0.416 },
{ h: 296, s: 0.575, v: 0.416 },
{ h: 311, s: 0.575, v: 0.416 },
{ h: 326, s: 0.575, v: 0.416 },
{ h: 341, s: 0.575, v: 0.416 },
];
const actual = analagousTight(base);

expect(actual).toEqual(expected);
});

test("analagousTight should return an array of complentary colors, reformatting to the given color space.", () => {
const { analagousTight } = harmonies;

const base = { r: 56, g: 45, b: 106 };

const expected = [
{ r: 56, g: 45, b: 106 },
{ r: 72, g: 45, b: 106 },
{ r: 87, g: 45, b: 106 },
{ r: 102, g: 45, b: 106 },
{ r: 106, g: 45, b: 95 },
{ r: 106, g: 45, b: 80 },
{ r: 106, g: 45, b: 64 },
];
const actual = analagousTight(base, "rgb");

expect(actual).toEqual(expected);
});

test("analagousTight should throw when passed invalid arguments.", () => {
const { analagousTight } = harmonies;

const badColor = null;
expect(() => {
analagousTight(badColor);
}).toThrow();

const goodColor = "#FFFFFF";
const badFormat = "CMYK";
expect(() => {
analagousTight(goodColor, badFormat);
}).toThrow();
});
});

0 comments on commit 98887d3

Please sign in to comment.