Browse Source

Add Color::asGrayscale()

Signed-off-by: falkTX <falktx@falktx.com>
pull/498/head
falkTX 2 months ago
parent
commit
e35799cb31
Signed by: falkTX <falktx@falktx.com> GPG Key ID: CDBAA37ABC74FBA0
2 changed files with 13 additions and 0 deletions
  1. +5
    -0
      dgl/Color.hpp
  2. +8
    -0
      dgl/src/Color.cpp

+ 5
- 0
dgl/Color.hpp View File

@@ -99,6 +99,11 @@ struct Color {
*/
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.
Values must in [0..1] range.


+ 8
- 0
dgl/src/Color.cpp View File

@@ -172,6 +172,14 @@ Color Color::invert() const noexcept
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)
{
float m1, m2;


Loading…
Cancel
Save