Skip to content

Commit

Permalink
Add and rollout osc::Rgba<T> and osc::ColorComponent, to generalize C…
Browse files Browse the repository at this point in the history
…olor's storage type
  • Loading branch information
adamkewley committed Aug 29, 2024
1 parent c5152da commit fd444f5
Show file tree
Hide file tree
Showing 20 changed files with 493 additions and 343 deletions.
1 change: 0 additions & 1 deletion src/OpenSimCreator/UI/Shared/ModelEditorViewerPanel.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
#include <memory>
#include <string_view>

namespace osc { struct Color; }
namespace osc { class CustomRenderingOptions; }
namespace osc { class ModelEditorViewerPanelLayer; }
namespace osc { class ModelEditorViewerPanelParameters; }
Expand Down
2 changes: 1 addition & 1 deletion src/OpenSimCreator/Utils/SimTKHelpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <SimTKcommon/internal/MassProperties.h>
#include <SimTKcommon/internal/Rotation.h>
#include <SimTKcommon/internal/Transform.h>
#include <oscar/Graphics/Color.h>
#include <oscar/Maths/EulerAngles.h>
#include <oscar/Maths/Mat3.h>
#include <oscar/Maths/Mat4.h>
Expand All @@ -13,7 +14,6 @@

#include <array>

namespace osc { struct Color; }
namespace osc { struct Transform; }

namespace osc
Expand Down
2 changes: 2 additions & 0 deletions src/oscar/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ add_library(oscar STATIC
Graphics/CameraProjection.h
Graphics/Color.cpp
Graphics/Color.h
Graphics/ColorComponent.h
Graphics/ColorHSLA.h
Graphics/ColorHSLA.cpp
Graphics/ColorRenderBufferFormat.h
Expand Down Expand Up @@ -171,6 +172,7 @@ add_library(oscar STATIC
Graphics/RenderTargetDepthStencilAttachment.h
Graphics/RenderTexture.h
Graphics/RenderTextureParams.h
Graphics/Rgba.h
Graphics/Scene.h
Graphics/Shader.h
Graphics/ShaderPropertyType.h
Expand Down
2 changes: 2 additions & 0 deletions src/oscar/Graphics.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <oscar/Graphics/CameraClippingPlanes.h>
#include <oscar/Graphics/CameraProjection.h>
#include <oscar/Graphics/Color.h>
#include <oscar/Graphics/ColorComponent.h>
#include <oscar/Graphics/ColorHSLA.h>
#include <oscar/Graphics/ColorRenderBufferFormat.h>
#include <oscar/Graphics/ColorRenderBufferParams.h>
Expand Down Expand Up @@ -38,6 +39,7 @@
#include <oscar/Graphics/RenderTargetDepthStencilAttachment.h>
#include <oscar/Graphics/RenderTexture.h>
#include <oscar/Graphics/RenderTextureParams.h>
#include <oscar/Graphics/Rgba.h>
#include <oscar/Graphics/Scene.h>
#include <oscar/Graphics/Shader.h>
#include <oscar/Graphics/ShaderPropertyType.h>
Expand Down
10 changes: 0 additions & 10 deletions src/oscar/Graphics/Color.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,6 @@ namespace
}
}

std::ostream& osc::operator<<(std::ostream& out, const Color& color)
{
return out << "Color(r = " << color.r << ", g = " << color.g << ", b = " << color.b << ", a = " << color.a << ')';
}

// the sRGB <--> linear relationship is commonly simplified to:
//
// - linear = sRGB ^ 2.2
Expand Down Expand Up @@ -137,11 +132,6 @@ Color osc::lerp(const Color& a, const Color& b, float t)
return Color{lerp(Vec4{a}, Vec4{b}, saturate(t))};
}

size_t std::hash<osc::Color>::operator()(const osc::Color& color) const
{
return hash_of(color.r, color.g, color.b, color.a);
}

Color32 osc::to_color32(const Color& color)
{
return to_color32(static_cast<Vec4>(color));
Expand Down
268 changes: 2 additions & 266 deletions src/oscar/Graphics/Color.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <oscar/Graphics/Color32.h>
#include <oscar/Graphics/ColorHSLA.h>
#include <oscar/Graphics/Rgba.h>
#include <oscar/Maths/Vec3.h>
#include <oscar/Maths/Vec4.h>

Expand All @@ -14,229 +15,7 @@

namespace osc
{
struct Color final {
using value_type = float;
using reference = float&;
using const_reference = const float&;
using size_type = size_t;
using iterator = value_type*;
using const_iterator = const value_type*;

static constexpr Color light_grey()
{
return {0.7f, 0.7f, 0.7f};
}

static constexpr Color dark_grey()
{
return {0.5f, 0.5f, 0.5f};
}

static constexpr Color half_grey()
{
return {0.5f, 0.5f, 0.5f};
}

static constexpr Color black()
{
return {0.0f, 0.0f, 0.0f};
}

static constexpr Color blue()
{
return {0.0f, 0.0f, 1.0f};
}

static constexpr Color muted_blue()
{
return {0.06f, 0.53f, 0.98f};
}

static constexpr Color clear()
{
return {0.0f, 0.0f, 0.0f, 0.0f};
}

static constexpr Color green()
{
return {0.0f, 1.0f, 0.0f};
}

static constexpr Color muted_green()
{
return {0.5f, 1.0f, 0.5f};
}

static constexpr Color dark_green()
{
return {0.0f, 0.6f, 0.0f};
}

static constexpr Color red()
{
return {1.0f, 0.0f, 0.0f};
}

static constexpr Color muted_red()
{
return {1.0f, 0.5f, 0.5f};
}

static constexpr Color white()
{
return {1.0f, 1.0f, 1.0f};
}

static constexpr Color yellow()
{
return {1.0f, 1.0f, 0.0f};
}

static constexpr Color muted_yellow()
{
return {1.0f, 1.0f, 0.6f};
}

static constexpr Color orange()
{
return {255.0f/255.0f, 165.0f/255.0f, 0.0f};
}

static constexpr Color cyan()
{
return {0.0f, 1.0f, 1.0f};
}

static constexpr Color magenta()
{
return {1.0f, 0.0f, 1.0f};
}

static constexpr Color purple()
{
return {191.0f/255.0f, 85.0f/255.0f, 236.0f/255.0f};
}

Color() = default;

explicit constexpr Color(value_type v) :
r{v}, g{v}, b{v}, a(1.0f)
{}

constexpr Color(value_type v, value_type alpha) :
r{v}, g{v}, b{v}, a{alpha}
{}

explicit constexpr Color(const Vec<3, value_type>& v) :
r{v.x}, g{v.y}, b{v.z}, a(1.0f)
{}

constexpr Color(const Vec<3, value_type>& v, value_type alpha) :
r{v.x}, g{v.y}, b{v.z}, a{alpha}
{}

explicit constexpr Color(const Vec<4, value_type>& v) :
r{v.x}, g{v.y}, b{v.z}, a{v.w}
{}

constexpr Color(value_type r_, value_type g_, value_type b_, value_type a_) :
r{r_}, g{g_}, b{b_}, a{a_}
{}

constexpr Color(value_type r_, value_type g_, value_type b_) :
r{r_}, g{g_}, b{b_}, a(1.0f)
{}

constexpr reference operator[](size_type pos)
{
static_assert(sizeof(Color) == 4*sizeof(value_type));
return (&r)[pos];
}

constexpr const_reference operator[](size_type pos) const
{
static_assert(sizeof(Color) == 4*sizeof(value_type));
return (&r)[pos];
}

constexpr size_t size() const
{
return 4;
}

constexpr iterator begin()
{
return &r;
}

constexpr iterator end()
{
return &r + size();
}

constexpr const_iterator begin() const
{
return &r;
}

constexpr const_iterator end() const
{
return &r + size();
}

constexpr operator Vec<4, value_type> () const
{
return Vec4{r, g, b, a};
}

friend bool operator==(const Color&, const Color&) = default;

constexpr friend Color& operator*=(Color& lhs, const Color& rhs)
{
lhs.r *= rhs.r;
lhs.g *= rhs.g;
lhs.b *= rhs.b;
lhs.a *= rhs.a;

return lhs;
}

constexpr friend Color operator*(const Color& lhs, const Color& rhs)
{
Color copy{lhs};
copy *= rhs;
return copy;
}

constexpr friend Color operator*(value_type lhs, const Color& rhs)
{
return Color{
lhs * rhs.r,
lhs * rhs.g,
lhs * rhs.b,
lhs * rhs.a,
};
}

constexpr Color with_alpha(value_type a_) const
{
return Color{r, g, b, a_};
}

constexpr Color with_element(size_type pos, value_type value) const
{
Color copy{*this};
copy[pos] = value;
return copy;
}

value_type r{};
value_type g{};
value_type b{};
value_type a{};
};

std::ostream& operator<<(std::ostream&, const Color&);
using Color = Rgba<float>;

// returns the linearized version of a sRGB component value
float to_linear_colorspace(float srgb_component_value);
Expand Down Expand Up @@ -265,18 +44,6 @@ namespace osc
return Vec4{color};
}

// returns a pointer to the first float element in the color
constexpr const float* value_ptr(const Color& color)
{
return &color.r;
}

// returns a pointer to the first float element in the color
constexpr float* value_ptr(Color& color)
{
return &color.r;
}

// linearly interpolates all components of `a` and `b` by the interpolant `t`
//
// `t` is clamped to [0.0f, 1.0f]. When `t` is 0, returns `a`. When `t` is 1, returns `b`
Expand Down Expand Up @@ -306,35 +73,4 @@ namespace osc
// returns a color that is the result of converting `color` to HSLA,
// multiplying its luminance (L) by `factor`, and converting it back to RGBA
Color multiply_luminance(const Color& color, float factor);

// when handled as a tuple-like object, a `Color` decomposes into its components (incl. alpha)

template<size_t I>
constexpr const float& get(const Color& color) { return color[I]; }

template<size_t I>
constexpr float& get(Color& color) { return color[I]; }

template<size_t I>
constexpr float&& get(Color&& color) { return std::move(color[I]); }

template<size_t I>
constexpr const float&& get(const Color&& color) { return std::move(color[I]); }
}

// define compile-time size for Color (same as `std::array`, `std::tuple`, `osc::Vec`, etc.)
template<>
struct std::tuple_size<osc::Color> {
static inline constexpr size_t value = 4;
};

template<size_t I>
struct std::tuple_element<I, osc::Color> {
using type = float;
};

// define hashing function for colors
template<>
struct std::hash<osc::Color> final {
size_t operator()(const osc::Color&) const;
};
Loading

0 comments on commit fd444f5

Please sign in to comment.