Skip to content

Commit

Permalink
Show gamut in sliders
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesnw committed Dec 10, 2024
1 parent 2fbf5f0 commit f90ce41
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
13 changes: 12 additions & 1 deletion src/lib/components/colors/Sliders.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@
style={`--stops: ${alphaGradient}`}
value={$color.alpha}
oninput={(e) => handleInput(e)}
data-channel="alpha"
/>
</div>
</div>
Expand All @@ -104,7 +105,17 @@
margin: 0;
display: block;
appearance: none;
background: linear-gradient(to right, var(--stops));
background: linear-gradient(to right, var(--stops)),
repeating-linear-gradient(
-45deg,
white 0%,
white 1%,
#ffd0d0 1%,
#ffd0d0 2%
);
&[data-channel='alpha'] {
background: linear-gradient(to right, var(--stops));
}
}
[data-group~='sliders'] {
Expand Down
28 changes: 27 additions & 1 deletion src/lib/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
clone,
display,
inGamut,
type PlainColorObject,
serialize,
set,
Expand Down Expand Up @@ -32,9 +33,34 @@ export const sliderGradient = (
steps: 10,
space: color.space,
hue: 'raw',
maxDeltaE: 10,
});
let wasInGamut = false;
const inGamutSteps: string[] = [];
const stepWidth = 100 / (gradientSteps.length - 1);

return gradientSteps.map((c) => display(c)).join(', ');
if (channel === 'alpha') {
return gradientSteps.map((c) => display(c)).join(', ');
}

gradientSteps.forEach((step, index) => {
if (inGamut(step, 'p3')) {
if (!wasInGamut) {
inGamutSteps.push(`transparent ${stepWidth * (index + 1)}%`);
}
wasInGamut = true;
inGamutSteps.push(`${display(step)} ${stepWidth * index}%`);
} else {
if (wasInGamut) {
inGamutSteps.push(`transparent ${stepWidth * (index - 1)}%`);
}
inGamutSteps.push(`transparent ${stepWidth * index}%`);

wasInGamut = false;
}
});

return inGamutSteps.join(', ');
};

function decodeColor(colorHash: string, format: ColorFormatId) {
Expand Down

0 comments on commit f90ce41

Please sign in to comment.