Skip to content

Commit

Permalink
Add Color::asGrayscale()
Browse files Browse the repository at this point in the history
Signed-off-by: falkTX <falktx@falktx.com>
  • Loading branch information
falkTX committed Feb 11, 2025
1 parent a8b7ef9 commit e35799c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
5 changes: 5 additions & 0 deletions dgl/Color.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
8 changes: 8 additions & 0 deletions dgl/src/Color.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit e35799c

Please sign in to comment.