d3 / d3-color

Color spaces! RGB, HSL, Cubehelix, CIELAB, and more.
https://d3js.org/d3-color
ISC License
400 stars 91 forks source link

Brightening black in RGB #80

Closed maltebaer closed 4 years ago

maltebaer commented 4 years ago

What is the desired behaviour of brightening black colour? Right now it will return black.

const brighter = d3.rgb("#000000").brighter(); // {b: 0, g: 0, opacity: 1, r: 0}
Fil commented 4 years ago

Interesting question. https://github.com/d3/d3-color#color_brighter says “The behavior of this method [brighter] is dependent on the implementing color space.”

and indeed d3.lch(d3.rgb("#000")).brighter().hex() // "#2c2c2c"

danburzo commented 4 years ago

There's no "official" definition of a brighten function, and as @Fil mentions, the behavior depends on the color space. In RGB, the effect on black follows the definiton: C' = C * Math.pow(1.42, k). This mostly corresponds to how the CSS brightness filter works (C' = slope * C + intercept); it, too, leaves black intact.

If you wanted more flexibility in applying transforms to colors (to obtain various brightness/contrast-type adjustments), the CSS Filter Effects Module may be a good start: In addition to a linear multiplier, you can also apply a gamma multiplier: C' = amplitude * Math.pow(C, exponent) + offset.

maltebaer commented 4 years ago

Thanks for your answers.

Converting to HCL color space will work as desired, in my scenario.