Skip to content

Commit

Permalink
Let operator"" _rgb return jngl::Rgb instead of jngl::Color
Browse files Browse the repository at this point in the history
  • Loading branch information
jhasse committed Nov 11, 2024
1 parent 63ddaf8 commit 95b3234
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 23 deletions.
9 changes: 0 additions & 9 deletions src/jngl/Color.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,6 @@ void Color::setBlue(const unsigned char blue) {
this->blue = blue;
}

Color::operator Rgb() const {
return Rgb::u8(red, green, blue);
}

Color interpolate(Color a, Color b, float t) {
return { static_cast<unsigned char>(static_cast<float>(a.getRed()) * (1.f - t) +
static_cast<float>(b.getRed()) * t),
Expand All @@ -49,8 +45,3 @@ Color interpolate(Color a, Color b, float t) {
}

} // namespace jngl

jngl::Color operator"" _rgb(const unsigned long long hex) {
return { static_cast<unsigned char>((hex >> 16) % 256),
static_cast<unsigned char>((hex >> 8) % 256), static_cast<unsigned char>(hex % 256) };
}
6 changes: 0 additions & 6 deletions src/jngl/Color.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,6 @@ class Color {
/// 0...255
void setBlue(unsigned char);

/// Implicit conversion for backwards compatibility
operator Rgb() const; // NOLINT

private:
unsigned char red;
unsigned char green;
Expand All @@ -68,6 +65,3 @@ void setBackgroundColor(jngl::Rgb);
void setBackgroundColor(unsigned char red, unsigned char green, unsigned char blue);

} // namespace jngl

/// Create a jngl::Color object from a literal. E.g. `0x00ff00_rgb` for green.
jngl::Color operator"" _rgb(unsigned long long);
6 changes: 2 additions & 4 deletions src/jngl/FrameBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,11 +182,9 @@ void FrameBuffer::Context::clear() {
glClear(GL_COLOR_BUFFER_BIT);
}

void FrameBuffer::Context::clear(const Color color) {
void FrameBuffer::Context::clear(const Rgb color) {
assert(resetCallback);
glClearColor(static_cast<float>(color.getRed()) / 255.f,
static_cast<float>(color.getGreen()) / 255.f,
static_cast<float>(color.getBlue()) / 255.f, 1);
glClearColor(color.getRed(), color.getGreen(), color.getBlue(), 1);
glClear(GL_COLOR_BUFFER_BIT);
}

Expand Down
2 changes: 1 addition & 1 deletion src/jngl/FrameBuffer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class FrameBuffer {
void clear();

/// Clear the framebuffer with \a color
void clear(Color color);
void clear(Rgb color);

private:
std::function<void()> resetCallback;
Expand Down
19 changes: 19 additions & 0 deletions src/jngl/Rgb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// For conditions of distribution and use, see copyright notice in LICENSE.txt
#include "Rgb.hpp"

#include "Color.hpp"

namespace jngl {

Rgb::Rgb(float red, float green, float blue) : red(red), green(green), blue(blue) {
Expand All @@ -12,6 +14,12 @@ Rgb Rgb::u8(uint8_t red, uint8_t green, uint8_t blue) {
static_cast<float>(blue) / 255.f };
}

Rgb::Rgb(Color color)
: red(static_cast<float>(color.getRed()) / 255.f),
green(static_cast<float>(color.getGreen()) / 255.f),
blue(static_cast<float>(color.getBlue()) / 255.f) {
}

float Rgb::getRed() const {
return red;
}
Expand All @@ -36,9 +44,20 @@ void Rgb::setBlue(const float blue) {
this->blue = blue;
}

Rgb::operator Color() const {
return Color{ static_cast<unsigned char>(red * 255), static_cast<unsigned char>(green * 255),
static_cast<unsigned char>(blue * 255) };
}

Rgb interpolate(Rgb a, Rgb b, float t) {
return { a.getRed() * (1.f - t) + b.getRed() * t, a.getGreen() * (1.f - t) + b.getGreen() * t,
a.getBlue() * (1.f - t) + b.getBlue() * t };
}

} // namespace jngl

jngl::Rgb operator"" _rgb(const unsigned long long hex) {
return jngl::Rgb::u8(static_cast<unsigned char>((hex >> 16) % 256),
static_cast<unsigned char>((hex >> 8) % 256),
static_cast<unsigned char>(hex % 256));
}
12 changes: 12 additions & 0 deletions src/jngl/Rgb.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
#include <cstdint>

namespace jngl {

class Color;

/// Object representing a RGB color, new version of jngl::Color (which will be deprecated in the
/// future)
class Rgb {
Expand All @@ -20,6 +23,9 @@ class Rgb {

static Rgb u8(uint8_t red, uint8_t green, uint8_t blue);

/// Implicit conversion for backwards compatibility
Rgb(Color); // NOLINT

/// 0.0f ... 1.0f
float getRed() const;
/// 0.0f ... 1.0f
Expand All @@ -35,6 +41,9 @@ class Rgb {
/// 0.0f ... 1.0f
void setBlue(float);

/// Conversion for backwards compatibility
explicit operator Color() const;

private:
float red;
float green;
Expand All @@ -45,3 +54,6 @@ class Rgb {
Rgb interpolate(Rgb a, Rgb b, float t);

} // namespace jngl

/// Create a jngl::Rgb object from a literal. E.g. `0x00ff00_rgb` for green.
jngl::Rgb operator"" _rgb(unsigned long long);
2 changes: 1 addition & 1 deletion src/jngl/shapes.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ void drawRect(Vec2 position, Vec2 size);
/// Draws a rectangle spawning from (0, 0) to (size.x, size.y) with the specified color
///
/// Use setAlpha to set the opacity.
void drawRect(const Mat3& modelview, Vec2 size, Color);
void drawRect(const Mat3& modelview, Vec2 size, Rgb);

/// Draws a rectangle spawning from (0, 0) to (size.x, size.y) with the specified color
///
Expand Down
2 changes: 1 addition & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,7 @@ void drawRect(const Vec2 position, const Vec2 size) {
pWindow->drawRect(position, size);
}

void drawRect(const Mat3& modelview, const Vec2 size, const Color color) {
void drawRect(const Mat3& modelview, const Vec2 size, const Rgb color) {
pWindow->drawRect(modelview, size, Rgba(color, Alpha(gShapeColor.getAlpha())));
}

Expand Down
2 changes: 1 addition & 1 deletion src/unittest/ColorTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace {
boost::ut::suite _ = [] {
using namespace boost::ut; // NOLINT
"Color"_test = [] {
const auto a = 0x68da4f_rgb;
const jngl::Color a{0x68da4f_rgb};
const auto b = jngl::Color(0x68, 0xda, 0x4f);
const auto c = jngl::Color(104, 218, 79);
expect(eq(a.getRed(), b.getRed()));
Expand Down

0 comments on commit 95b3234

Please sign in to comment.