Skip to content

How can I obtain a color with a specific lightness from the interpolator? #181

Closed Answered by danburzo
rptfrg asked this question in Q&A
Discussion options

You must be logged in to vote

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 Lightness = 0.3, find two adjacent colors in your array whose Lightness interval contains 0.3.

const interval_index = colors.findIndex(function(color, i) {
  return (
    i < colors.length - 1 && 
    Math.min(color.l, colors[i+1].l)

Replies: 1 comment 4 replies

Comment options

You must be logged in to vote
4 replies
@rptfrg
Comment options

@rptfrg
Comment options

@danburzo
Comment options

@rptfrg
Comment options

Answer selected by rptfrg
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants