| @@ -99,6 +99,11 @@ struct Color { | |||||
| */ | */ | ||||
| Color invert() const noexcept; | Color invert() const noexcept; | ||||
| /** | |||||
| Create a new color based on this one but in grayscale (using weighted average). | |||||
| */ | |||||
| Color asGrayscale() const noexcept; | |||||
| /** | /** | ||||
| Create a color specified by hue, saturation and lightness. | Create a color specified by hue, saturation and lightness. | ||||
| Values must in [0..1] range. | Values must in [0..1] range. | ||||
| @@ -172,6 +172,14 @@ Color Color::invert() const noexcept | |||||
| return color; | return color; | ||||
| } | } | ||||
| Color Color::asGrayscale() const noexcept | |||||
| { | |||||
| Color color(*this); | |||||
| // values taken from https://goodcalculators.com/rgb-to-grayscale-conversion-calculator/ | |||||
| color.red = color.green = color.blue = 0.299f * color.red + 0.587f * color.green + 0.114f * color.blue; | |||||
| return color; | |||||
| } | |||||
| Color Color::fromHSL(float hue, float saturation, float lightness, float alpha) | Color Color::fromHSL(float hue, float saturation, float lightness, float alpha) | ||||
| { | { | ||||
| float m1, m2; | float m1, m2; | ||||