-
-
Notifications
You must be signed in to change notification settings - Fork 51
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Relative lighten/darken #90
Comments
@michal-kurz, thank you for the idea, I think this is a valid point. |
@EricRovell thanks for the great PR! @omgovich thanks for your incredible work! Do you have an interest in merging that PR? (#91)? We're also moving from |
I think a relative saturate feature would also be useful. Perhaps this is not necessarily best practice in color manipulation or something, but converting from Qix's Qix's library does the math like this: saturate(ratio) {
const hsl = this.hsl();
hsl.color[1] += hsl.color[1] * ratio;
return hsl;
}, Assuming I did my math right, the outputs vary as below: I really want to switch to I know |
@slapbox the author is quite busy these days, unfortunately. I think if you need this PR you can extend the Colord class and override the functionality for your needs🤔 |
@EricRovell makes sense! It would be really great to get your PR merged soon, but short of that, maybe consider releasing a plugin for your PR. If I make a plugin for relative saturate, I'll do the same. Even just a |
Here's a very basic solution for anyone who needs it. It possibly has some mistake in it, as our app still doesn't look as expected, but perhaps that's some other unrelated issue. import { Colord } from 'colord';
// Copied from colord utils
export const clamp = (number: number, min = 0, max = 1): number =>
number > max ? max : number > min ? number : min;
// Copied from https://github.com/Qix-/color/blob/master/index.js
function saturateRelative(hslColor, ratio) {
const { s: saturation } = hslColor;
const newSaturation = saturation + saturation * ratio;
return { ...hslColor, s: newSaturation };
}
// Copied from https://github.com/Qix-/color/blob/master/index.js
function desaturateRelative(hslColor, ratio) {
const { s: saturation } = hslColor;
const newSaturation = saturation - saturation * ratio;
return { ...hslColor, s: newSaturation };
}
const relativeSaturatePlugin = ColordClass => {
ColordClass.prototype.saturate = function (ratio = 1) {
const mixture = saturateRelative(this.toHsl(), ratio);
return new ColordClass(mixture);
};
ColordClass.prototype.desaturate = function (ratio = 1) {
const mixture = desaturateRelative(this.toHsl(), ratio);
return new ColordClass(mixture);
};
};
function lightenDarken(hslColor, amount, relative) {
const l = relative ? hslColor.l * (1 + amount) : hslColor.l + amount * 100;
return { ...hslColor, l: clamp(l, 0, 100) };
}
const relativeLightenDarkenPlugin = ColordClass => {
ColordClass.prototype.lightenRel = function (amount, relative = true) {
const adjusted = lightenDarken(this.toHsl(), amount, relative);
return new ColordClass(adjusted);
};
ColordClass.prototype.darkenRel = function (amount, relative = true) {
const adjusted = lightenDarken(this.toHsl(), 0 - amount, relative);
return new ColordClass(adjusted);
};
};
export { relativeSaturatePlugin, relativeLightenDarkenPlugin }; |
I replaced
color
withcolord
in my project, but found out I don't get the same results, because they treatlighten()
anddarken()
differently: Whilecolor
shifts color by a fraction of itself, whilecolord
shifts color by a fraction of whole spectrum.I prototyped this wrapper to implement this functionality:
and I will use it for now, but I'd prefer to merge it into the library itself - are you interested in this functionality? I would love to make a PR if so.
Thank you 🙏
The text was updated successfully, but these errors were encountered: