Skip to content

Commit

Permalink
Ignore invalid colors
Browse files Browse the repository at this point in the history
Fixes: #2
  • Loading branch information
darekkay committed Nov 14, 2020
1 parent a3053f1 commit 4554c74
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 13 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## [Unreleased]

- :rocket: Ignore invalid colors.
- :hammer: Replace TravisCI with GitHub Actions.

## [1.1.0] - 2020-10-28
Expand Down
15 changes: 8 additions & 7 deletions src/__tests__/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const onecolor = require("onecolor");
const { isFlat, flattenPalette, normalizePalette } = require("../index");

const flatPalette = {
Expand Down Expand Up @@ -27,13 +28,13 @@ const nestedPalette = {
};

const normalizedPalette = [
{ family: "light-blue", grade: 10, hex: "#d9e8f6" },
{ family: "light-blue", grade: 20, hex: "#aacdec" },
{ family: "green", grade: 10, hex: "#dfeacd" },
{ family: "green", grade: 20, hex: "#b8d293" },
{ family: "white", grade: 0, hex: "#ffffff" },
{ family: "black", grade: 100, hex: "#000000" },
];
{ family: "light-blue", grade: 10, value: "#d9e8f6" },
{ family: "light-blue", grade: 20, value: "#aacdec" },
{ family: "green", grade: 10, value: "#dfeacd" },
{ family: "green", grade: 20, value: "#b8d293" },
{ family: "white", grade: 0, value: "#ffffff" },
{ family: "black", grade: 100, value: "#000000" },
].map((color) => ({ ...color, onecolorValue: onecolor(color.value) }));

test("isFlat returns true for flat color palette definition", () => {
expect(isFlat(flatPalette)).toBe(true);
Expand Down
25 changes: 19 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const verifyContrastRatio = (palette, magicNumber) => {

palette.forEach((color1) => {
palette.forEach((color2) => {
const contrastRatio = onecolor(color1.hex).contrast(onecolor(color2.hex));
const contrastRatio = color1.onecolorValue.contrast(color2.onecolorValue);

if (color1.grade > color2.grade) {
return; // don't compare same colors to each other
Expand Down Expand Up @@ -68,28 +68,41 @@ const flattenPalette = (nestedPalette) =>
{}
);

/** Create an array of { family, grade, hex } values */
/** Create an array of { family, grade, value } values */
const normalizePalette = (flatPalette) =>
Object.entries(flatPalette).reduce(
(accumulator2, [name, hex]) => [
(accumulator2, [name, value]) => [
...accumulator2,
{
family: name.substring(0, name.lastIndexOf("-")),
grade: parseInt(name.substring(name.lastIndexOf("-") + 1), 10),
hex,
value,
onecolorValue: onecolor(value.trim()),
},
],
[]
);

const removeInvalidColors = (palette) => {
return palette.filter((color) => {
if (!color.onecolorValue) {
logger.error(
`Color ${color.family}-${color.grade} is invalid: '${color.value}'`
);
}
return !!color.onecolorValue;
});
};

const run = (colors, config) => {
logger.info(`${green("Analyzing")}: ${config.file}`);

const flatPalette = isFlat(colors) ? colors : flattenPalette(colors);
const normalizedPalette = normalizePalette(flatPalette);
const magicNumbers = calculateMagicNumbers(normalizedPalette);
const validPalette = removeInvalidColors(normalizedPalette);
const magicNumbers = calculateMagicNumbers(validPalette);

logger.log(`${green("Colors:")} ${normalizedPalette.length}`);
logger.log(`${green("Colors:")} ${validPalette.length}`);
logger.log(green("Magic numbers:"));
Object.entries(magicNumbers).forEach(([grade, magicNumber]) => {
logger.log(
Expand Down

0 comments on commit 4554c74

Please sign in to comment.