-
Let's say I've got myself a nice Is there any way I can obtain a color from it with a condition that, for example, Thank you! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
Depending on the kinds of constrains you are able to impose, there might be a couple of different solutions, none of them immediately using the culori API. Assuming you're using the (default) linear interpolation, it can be solved mathematically. import { okhsl } from 'culori';
const colors = ['white', 'yellow', 'orange', 'red', black'].map(okhsl);
const condition_L = 0.3; First, find the two colors between which your desired color resides. If you're looking for const interval_index = colors.findIndex(function(color, i) {
return (
i < colors.length - 1 &&
Math.min(color.l, colors[i+1].l) <= condition_L &&
Math.max(color.l, colors[i+1].l) >= condition_L
);
});
if (interval_index > -1) {
const colorA = colors[interval_index];
const colorB = colors[interval_index + 1];
} Then, having found your import { lerp } from 'culori';
const t = colorA.l === colorB.l ? 0 : (condition_L - colorA.l) / (colorB.l - colorA.l);
const desired_color = {
mode: 'okhsl',
h: lerp(colorA.h, colorB.h, t),
s: lerp(colorA.s, colorB.s, t),
l: condition_L
} This code, which I've written here on GitHub for illustrative purposes (without testing) just scratches the surface, but hopefully gets you on the right track to solve the problem! |
Beta Was this translation helpful? Give feedback.
Depending on the kinds of constrains you are able to impose, there might be a couple of different solutions, none of them immediately using the culori API. Assuming you're using the (default) linear interpolation, it can be solved mathematically.
First, find the two colors between which your desired color resides. If you're looking for
Lightness = 0.3
, find two adjacent colors in your array whose Lightness interval contains0.3
.