Skip to content

Commit

Permalink
Fix distribute
Browse files Browse the repository at this point in the history
  • Loading branch information
mcnuttandrew committed Oct 13, 2024
1 parent 031dc28 commit 8b4e216
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 5 deletions.
19 changes: 18 additions & 1 deletion packages/palette/src/Color.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { expect, test } from "vitest";
import { Color, ColorSpaceDirectory } from "color-buddy-palette";
import {
Color,
ColorSpaceDirectory,
distributePoints,
} from "color-buddy-palette";

test("Color string extractor works", () => {
expect(Color.stringToChannels("lab", "lab(50% 0 0)")).toStrictEqual([
Expand Down Expand Up @@ -99,3 +103,16 @@ test("In gamut tests", () => {
});
});
});

test.only("Distribute colors", () => {
const exampleColors = ["#072536", "#144327", "#dea1db", "#c6338f", "#822b21"];
const colors = exampleColors.map((hex) => Color.colorFromHex(hex, "hsl"));

const result = distributePoints(
{ direction: "in z space", name: "L" },
exampleColors.map((_, idx) => idx),
colors,
"hsl"
);
expect(result.map((x) => x.toHex())).toMatchSnapshot();
});
11 changes: 11 additions & 0 deletions packages/palette/src/__snapshots__/Color.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`Distribute colors 1`] = `
[
"#072536",
"#216d3f",
"#dea1db",
"#d55aa7",
"#b13b2d",
]
`;
11 changes: 7 additions & 4 deletions packages/palette/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,14 @@ export function distributePoints(
yChannelIndex: space.channelNames.indexOf(y),
zChannelIndex: space.channelNames.indexOf(z),
};
const idxMap = {
horizontal: config.xChannelIndex,
vertical: config.yChannelIndex,
"in z space": config.zChannelIndex,
} as const;
let sortedIndexes = focusedColors.sort((a, b) => {
const modeToIdx = { horizontal: 1, vertical: 2, "in z space": 0 };
const idx = modeToIdx[dir.direction] || 0;
const pointA = colors[a].toChannels()[idx];
const pointB = colors[b].toChannels()[idx];
const pointA = colors[a].toChannels()[idxMap[dir.direction]];
const pointB = colors[b].toChannels()[idxMap[dir.direction]];
return pointA - pointB;
});
type Channels = [number, number, number];
Expand Down

0 comments on commit 8b4e216

Please sign in to comment.