From 8b4e216e29945d8d1aabb1183cacda4bd5211aa0 Mon Sep 17 00:00:00 2001 From: Andrew Michael McNutt Date: Sun, 13 Oct 2024 17:22:57 -0600 Subject: [PATCH] Fix distribute --- packages/palette/src/Color.test.ts | 19 ++++++++++++++++++- .../src/__snapshots__/Color.test.ts.snap | 11 +++++++++++ packages/palette/src/utils.ts | 11 +++++++---- 3 files changed, 36 insertions(+), 5 deletions(-) create mode 100644 packages/palette/src/__snapshots__/Color.test.ts.snap diff --git a/packages/palette/src/Color.test.ts b/packages/palette/src/Color.test.ts index 4951878..2e14acc 100644 --- a/packages/palette/src/Color.test.ts +++ b/packages/palette/src/Color.test.ts @@ -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([ @@ -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(); +}); diff --git a/packages/palette/src/__snapshots__/Color.test.ts.snap b/packages/palette/src/__snapshots__/Color.test.ts.snap new file mode 100644 index 0000000..aea2c80 --- /dev/null +++ b/packages/palette/src/__snapshots__/Color.test.ts.snap @@ -0,0 +1,11 @@ +// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html + +exports[`Distribute colors 1`] = ` +[ + "#072536", + "#216d3f", + "#dea1db", + "#d55aa7", + "#b13b2d", +] +`; diff --git a/packages/palette/src/utils.ts b/packages/palette/src/utils.ts index 6d7be36..5e69cf1 100644 --- a/packages/palette/src/utils.ts +++ b/packages/palette/src/utils.ts @@ -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];