bgrins / TinyColor

Fast, small color manipulation and conversion for JavaScript
https://bgrins.github.io/TinyColor/
MIT License
5.05k stars 437 forks source link

add brightness function #39

Closed snird closed 10 years ago

snird commented 10 years ago

ok, this is quite tricky, at first I thought you had a bug in your lighten function but it appears to be just right to the "standard": http://en.wikipedia.org/wiki/HSL_and_HSV#Lightness but what I was really looking for was brighten function (according to the wikipedia page). given the hex code of black #000000 brighten by 50% should be #7f7f7f (while lighten gives back #808080) here, this tool: hexcolortool.com uses lighten like this. the brighten function is the "standard" lighten function in many places, for example in sass (which is why I was confused) and many, many standard libraries (e.g, even the Java library: http://www.java2s.com/Code/Java/2D-Graphics-GUI/Lightensacolorbyagivenamount.htm )

brighten function is really necessary, and should be explained to differentiate from the current lighten IMO. thanks.

bgrins commented 10 years ago

OK, thanks for the info. I'm looking at the Sass implementation here and their examples seem to match the current behavior: https://github.com/nex3/sass/blob/fda58a6c342afc3f6ba87fbfc0158cb1a00a768f/lib/sass/script/functions.rb#L897-L914. There are also more test cases here - I haven't ran them all to make sure it conforms: https://github.com/nex3/sass/blob/fda58a6c342afc3f6ba87fbfc0158cb1a00a768f/test/sass/functions_test.rb#L441-L449.

Here is the code I've run to compare with their comment:

tinycolor.lighten("hsl(0, 0%, 0%)", 30).toHslString()
"hsl(0, 0%, 30%)"

tinycolor.lighten("#800", 20).toHexString()
"#ee0000"
snird commented 10 years ago

yes, you are right. the sass implementation is the same as yours, my mistake. but there is some reason to implement the brighten function I suggested IMO, this technique yields somewhat different results and when working with another library somewhere else, which uses this technique, it might be useful to have this as well in tinycolor.

Radagaisus commented 10 years ago

@bgrins to add to this, lighten tweaks the color in two dimensions (saturation and lightness), while brighten only works on the lightness attribute.

bgrins commented 10 years ago

Agree that the new brighten function could be useful on its own.

@bgrins to add to this, lighten tweaks the color in two dimensions (saturation and lightness), while brighten only works on the lightness attribute.

@Radagaisus Sounds like you have the terms backwards here, right? lighten just changes the lightness in HSL space whereas brighten as implemented in #40 is working in RGB space and just increases all 3 components equally.