Skip to content
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

Colors - improvements to constructor, setters, and operators/blending. #639

Merged
merged 1 commit into from
Sep 20, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 37 additions & 3 deletions src/util/color.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*
/*
TODO:
- Add Blend(), Scale(), etc.
- I'd also like to change the way the Color names are accessed.
Expand All @@ -23,7 +23,8 @@ namespace daisy
class Color
{
public:
Color() {}
Color() : red_(0.f), green_(0.f), blue_(0.f) {}
Color(float r, float g, float b) : red_(r), green_(g), blue_(b) {}
~Color() {}

/** List of colors that have a preset RGB value */
Expand All @@ -40,7 +41,7 @@ class Color
LAST /**< & */
};

/** Initializes the Color with a given preset.
/** Initializes the Color with a given preset.
\param c Color to init to
*/
void Init(PresetColor c);
Expand All @@ -66,6 +67,10 @@ class Color
inline uint8_t Green8() const { return green_ * 255; }
inline uint8_t Blue8() const { return blue_ * 255; }

inline void SetRed(const float amt) { red_ = amt; }
inline void SetGreen(const float amt) { green_ = amt; }
inline void SetBlue(const float amt) { blue_ = amt; }

/** Returns a scaled color by a float */
Color operator*(float scale)
{
Expand All @@ -74,6 +79,35 @@ class Color
return c;
}

/** Adds another color to this one, destructively saturating at 1 */
Color operator+(Color rhs)
{
float r_ = red_ + rhs.Red();
float g_ = green_ + rhs.Green();
float b_ = blue_ + rhs.Blue();
if(r_ > 1.f)
r_ = 1.f;
if(g_ > 1.f)
g_ = 1.f;
if(b_ > 1.f)
b_ = 1.f;
Color c(r_, g_, b_);
return c;
}

/** Returns a color that is blended between a and b */
static Color Blend(const Color a, const Color b, const float amt)
{
float scalar = amt > 1.f ? 1.f : amt < 0.f ? 0.f : amt;
float nr = a.Red() + (b.Red() - a.Red()) * scalar;
float ng = a.Green() + (b.Green() - a.Green()) * scalar;
float nb = a.Blue() + (b.Blue() - a.Blue()) * scalar;

Color new_color(nr, ng, nb);
return new_color;
}


private:
static const float standard_colors[LAST][3];
float red_, green_, blue_;
Expand Down
Loading