8000 Color spaces implementation by cone-forest · Pull Request #34 · 4J-company/mr-math · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Color spaces implementation #34

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open

Conversation

cone-forest
Copy link
Collaborator
@cone-forest cone-forest commented May 2, 2025
  • Rename mr::Color to mr::RGBAColor
  • Fix deprecated literal suffix syntax
  • First color spaces implementation for mr::Color

Summary by CodeRabbit

  • New Features

    • Introduced modular color types: RGBA, HSLA, HSVA, CMYK, and OKLAB with enhanced color space conversions and common operations.
  • Documentation

    • Updated usage examples to reference the new RGBA color type.
  • Style

    • Renamed types in debugger visualizations for improved clarity.
  • Tests

    • Updated tests to use the new RGBA color type and improved comment clarity.

Copy link
coderabbitai bot commented May 2, 2025

Walkthrough

The changes refactor the color handling system by replacing the monolithic Color struct with a modular set of color structs, including RGBAColor, HSVAColor, HSLAColor, CMYKColor, and OKLABColor. A CRTP-based ColorBase template is introduced to unify common color operations. The RGBAColor struct now serves as the primary color representation, with updated constructors and format conversion methods. A color conversion function for RGBA to OKLAB is implemented. All documentation, tests, and debugger visualization files are updated to use RGBAColor instead of the previous Color type, with no changes to the logic or semantics of the examples and tests.

Changes

File(s) Change Summary
include/mr-math/color.hpp Refactored color system: introduced ColorBase CRTP template, replaced Color with RGBAColor, added HSVAColor, HSLAColor, CMYKColor, OKLABColor, and implemented RGBA→OKLAB conversion. Updated user-defined literal and structured bindings.
USAGE.md Updated documentation to use mr::RGBAColor instead of mr::Color in all code examples and explanations.
mr-math.natvis Changed debugger visualization type from mr::Color to mr::RGBAColor.
tests/misc.cpp Updated all tests to use mr::RGBAColor instead of mr::Color. Renamed test cases and fixed a minor comment typo.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant RGBAColor
    participant OKLABColor

    User->>RGBAColor: Construct RGBAColor (from floats, ints, or hex)
    User->>RGBAColor: Access or modify color components
    User->>RGBAColor: Convert to other formats (argb, bgra, abgr)
    User->>OKLABColor: convert<RGBAColor, OKLABColor>(rgba)
    OKLABColor-->>User: Return OKLABColor instance
Loading

Poem

In fields of color, rabbits play,
With RGBA now leading the way.
From docs to tests, the change is clear,
Modular hues bring new frontiers!
OKLAB joins the bunny’s art,
With every hop, a vibrant start.
🐇🌈

✨ Finishing Touches
  • 📝 Generate Docstrings

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a 8000 pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 7

🔭 Outside diff range comments (2)
mr-math.natvis (1)

107-116: 🧹 Nitpick (assertive)

Spurious closing brace in the display string
R={r()}, G={g()}, B={b()}, A={a()}} has one brace too many, so Visual Studio will show an Invalid Expression error for the whole visualiser.

-<DisplayString>R={r()}, G={g()}, B={b()}, A={a()}}</DisplayString>
+<DisplayString>R={r()}, G={g()}, B={b()}, A={a()}</DisplayString>
USAGE.md (1)

220-220: 🧹 Nitpick (assertive)

Typo: epsionepsilon

-float eps = mr::epsion<float>();
+float eps = mr::epsilon<float>();
📜 Review details

Configuration used: CodeRabbit UI
Review profile: ASSERTIVE
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between f5efe00 and ae0812d.

📒 Files selected for processing (4)
  • USAGE.md (2 hunks)
  • include/mr-math/color.hpp (3 hunks)
  • mr-math.natvis (1 hunks)
  • tests/misc.cpp (3 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (2)
include/mr-math/color.hpp (3)
include/mr-math/matr.hpp (18)
  • other (117-126)
  • other (117-117)
  • other (128-133)
  • other (128-128)
  • other (135-140)
  • other (135-135)
  • other (142-150)
  • other (142-142)
  • other (152-158)
  • other (152-152)
  • other (160-166)
  • other (160-160)
  • nodiscard (169-171)
  • nodiscard (173-175)
  • nodiscard (178-191)
  • _data (321-324)
  • _data (383-386)
  • _data (455-458)
include/mr-math/operators.hpp (2)
  • s (133-141)
  • s (134-134)
include/mr-math/def.hpp (2)
  • epsilon (35-37)
  • epsilon (35-35)
tests/misc.cpp (1)
include/mr-math/color.hpp (6)
  • RGBAColor (76-76)
  • RGBAColor (79-80)
  • RGBAColor (82-83)
  • RGBAColor (86-93)
  • RGBAColor (95-101)
  • RGBAColor (95-95)
🪛 markdownlint-cli2 (0.17.2)
USAGE.md

190-190: Fenced code blocks should be surrounded by blank lines
null

(MD031, blanks-around-fences)


192-192: Fenced code blocks should be surrounded by blank lines
null

(MD031, blanks-around-fences)


197-197: Fenced code blocks should be surrounded by blank lines
null

(MD031, blanks-around-fences)


199-199: Lists should be surrounded by blank lines
null

(MD032, blanks-around-lists)


199-199: Lists should be surrounded by blank lines
null

(MD032, blanks-around-lists)


200-200: Fenced code blocks should be surrounded by blank lines
null

(MD031, blanks-around-fences)


205-205: Fenced code blocks should be surrounded by blank lines
null

(MD031, blanks-around-fences)


206-206: Lists should be surrounded by blank lines
null

(MD032, blanks-around-lists)


206-206: Lists should be surrounded by blank lines
null

(MD032, blanks-around-lists)


207-207: Fenced code blocks should be surrounded by blank lines
null

(MD031, blanks-around-fences)

🪛 GitHub Actions: Benchmarks
include/mr-math/color.hpp

[error] 29-54: Syntax errors due to invalid use of 'this' keyword in method parameters and undeclared identifiers 'self', 'other', and 'eps'.


[error] 213-213: No matching function for call to 'mr::math::Vec<float, 3>::Vec(mr::math::RGBAColor&)'. Invalid static_cast from RGBAColor to Vec3f.

⏰ Context from checks skipped due to timeout of 90000ms (3)
  • GitHub Check: build (windows-latest, Release, cl)
  • GitHub Check: build (windows-latest, Debug, cl)
  • GitHub Check: build (ubuntu-24.04, Debug, clang)
🔇 Additional comments (5)
include/mr-math/color.hpp (1)

213-230: convert<RGBAColor, OKLABColor> still fails without the implicit Vec3f conversion
After replacing the “deducing this” operators you must adjust this line:

auto v = static_cast<Vec3f>(rgba) * Matr3f{ … };

Because static_cast<Vec3f>(rgba) now works (see earlier fix) this part will compile, but note:

  • You need <cmath> for cbrtf.
  • Vec3f * Matr3f looks backwards – shouldn’t it be Matr3f * Vec3f? Double-check the intended order.

No code suggestion here, only a heads-up to verify the math once the build is green.

🧰 Tools
🪛 GitHub Actions: Benchmarks

[error] 213-213: No matching function for call to 'mr::math::Vec<float, 3>::Vec(mr::math::RGBAColor&)'. Invalid static_cast from RGBAColor to Vec3f.

tests/misc.cpp (4)

34-45: Tests updated to use mr::RGBAColor

All constructor tests have been correctly updated to reference the new RGBAColor type, including float, integer, and packed‐integer overloads and the user‐defined literal. The assertions align with the implementations in include/mr-math/color.hpp.


35-35: Verify default‐constructor behavior

The test assumes RGBAColor() yields (0,0,0,0). Please confirm that the default constructor zero‐initializes all channels (including alpha) rather than defaulting alpha to 1.0 or leaving uninitialized memory.


77-77: Setters test updated to RGBAColor

The setter test now correctly asserts the updated RGBA channel values against the RGBAColor instance. Good use of set(index, value) following r(…).


94-95: Clarify addition semantics and potential clamping

The test allows channel values to exceed 1.0 (e.g., alpha sums to 2.0). Please verify whether RGBAColor::operator+ is intended to produce unclamped results or should clamp channels to [0,1]. If clamping is desired, consider adding a saturating‐add function or adjust operator behavior.

Comment on lines +17 to +20
template <typename InputT, typename OutputT>
OutputT convert(InputT input) {
static_assert(false, "You have to specify this function for your particular types");
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

convert is currently unusable – the unconditional static_assert(false) aborts every build
With the present implementation the primary template can never be instantiated, so even a valid call (one that has a full specialization) will trigger the assertion before the compiler has a chance to pick the specialization. Make the assertion dependent on the template parameters:

-template <typename InputT, typename OutputT>
-  OutputT convert(InputT input) {
-    static_assert(false, "You have to specify this function for your particular types");
-  }
+template <typename...>
+inline constexpr bool _always_false_v = false;
+
+template <typename InputT, typename OutputT>
+constexpr OutputT convert(InputT /*unused*/) {
+  static_assert(_always_false_v<InputT, OutputT>,
+                "No `convert` specialization found for the supplied types");
+}

This stops the hard error, allows the compiler to look for specializations, and still provides a clear diagnostic when none are found.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
template <typename InputT, typename OutputT>
OutputT convert(InputT input) {
static_assert(false, "You have to specify this function for your particular types");
}
template <typename...>
inline constexpr bool _always_false_v = false;
template <typename InputT, typename OutputT>
constexpr OutputT convert(InputT /*unused*/) {
static_assert(_always_false_v<InputT, OutputT>,
"No `convert` specialization found for the supplied types");
}

Comment on lines +49 to +55
bool operator==(this const ColorDerived &self, const ColorDerived &other) noexcept {
return self._data == other._data;
}

bool equal(this const ColorDerived &self, const ColorDerived &other, ValueT eps = epsilon<ValueT>()) noexcept {
return self._data.equal(other._data, eps);
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

operator== / equal suffer from the same “deducing this” issue
Rewrite using the conventional form and preserve const-correctness:

-bool operator==(this const ColorDerived &self, const ColorDerived &other) noexcept {
-  return self._data == other._data;
-}
-
-bool equal(this const ColorDerived &self, const ColorDerived &other, ValueT eps = epsilon<ValueT>()) noexcept {
-  return self._data.equal(other._data, eps);
-}
+constexpr bool operator==(const ColorDerived &other) const noexcept {
+  return static_cast<const ColorDerived &>(*this)._data == other._data;
+}
+
+constexpr bool equal(const ColorDerived &other,
+                     ValueT eps = epsilon<ValueT>()) const noexcept {
+  return static_cast<const ColorDerived &>(*this)._data.equal(other._data, eps);
+}

This removes the compilation errors and keeps behaviour identical.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
bool operator==(this const ColorDerived &self, const ColorDerived &other) noexcept {
return self._data == other._data;
}
bool equal(this const ColorDerived &self, const ColorDerived &other, ValueT eps = epsilon<ValueT>()) noexcept {
return self._data.equal(other._data, eps);
}
constexpr bool operator==(const ColorDerived &other) const noexcept {
return static_cast<const ColorDerived &>(*this)._data == other._data;
}
constexpr bool equal(const ColorDerived &other,
ValueT eps = epsilon<ValueT>()) const noexcept {
return static_cast<const ColorDerived &>(*this)._data.equal(other._data, eps);
}

Comment on lines +27 to +38
// structured binding support
template <size_t I> requires (I < 4)
ValueT get(this const ColorDerived &self) { return self._data[I]; }

operator Vec3f(this ColorDerived &self) noexcept {
return (Vec3f)self._data;
}

operator Vec4f(this ColorDerived &self) noexcept {
return self._data;
}

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

deducing this syntax isn’t yet supported by our tool-chain – replace it with ordinary methods
All the functions that use
operator Vec3f(this ColorDerived &self) / ValueT get(this const ColorDerived &self) etc. are rejected by both GCC 13 and Clang 17 (see CI errors 29-54). A drop-in, C++20-compatible rewrite is straightforward:

-// structured binding support
-template <size_t I> requires (I < 4)
-  ValueT get(this const ColorDerived &self) { return self._data[I]; }
-
-operator Vec3f(this ColorDerived &self) noexcept {
-  return (Vec3f)self._data;
-}
-
-operator Vec4f(this ColorDerived &self) noexcept {
-  return self._data;
-}
+// structured binding support
+template <size_t I> requires (I < 4)
+constexpr ValueT get() const noexcept {
+  static_assert(I < 4);
+  return static_cast<const ColorDerived &>(*this)._data[I];
+}
+
+/* implicit conversions */
+constexpr operator Vec3f() const noexcept {
+  const auto &d = static_cast<const ColorDerived &>(*this)._data;
+  return Vec3f{d.x(), d.y(), d.z()};          // avoid the “no Vec3f(Vec4f) ctor” error
+}
+constexpr operator Vec4f() const noexcept {
+  return static_cast<const ColorDerived &>(*this)._data;
+}

Apply the same pattern to every other member that currently relies on the P0847 “deducing this” feature.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
// structured binding support
template <size_t I> requires (I < 4)
ValueT get(this const ColorDerived &self) { return self._data[I]; }
operator Vec3f(this ColorDerived &self) noexcept {
return (Vec3f)self._data;
}
operator Vec4f(this ColorDerived &self) noexcept {
return self._data;
}
// structured binding support
template <size_t I> requires (I < 4)
constexpr ValueT get() const noexcept {
static_assert(I < 4);
return static_cast<const ColorDerived &>(*this)._data[I];
}
/* implicit conversions */
constexpr operator Vec3f() const noexcept {
const auto &d = static_cast<const ColorDerived &>(*this)._data;
return Vec3f{d.x(), d.y(), d.z()}; // avoid the “no Vec3f(Vec4f) ctor” error
}
constexpr operator Vec4f() const noexcept {
return static_cast<const ColorDerived &>(*this)._data;
}

Comment on lines +57 to +62
friend std::ostream & operator<<(std::ostream &s, const ColorDerived &color) noexcept {
Vec4u comps = color._data * 255;
comps.clamp(0, 256);
s << '#' << std::hex << std::uppercase << comps[0] << comps[1] << comps[2] << comps[3] << std::nouppercase << std::dec;
return s;
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick (assertive)

operator<< prints incorrect hex and risks narrowing

  1. Multiplying a Vec4f by 255 gives floats; converting them to a Vec4u via implicit ctor may silently floor or truncate.
  2. << std::hex streams the decimal value of every component, not two-digit padded bytes, so #3F3F3F3F might appear as #3F3F3F3F or #3f3f3f3f depending on the values ‑ but e.g. component 10 becomes just A, losing the leading zero.

Consider formatting each channel explicitly:

-Vec4u comps = color._data * 255;
-comps.clamp(0, 256);
-s << '#' << std::hex << std::uppercase << comps[0] << comps[1] << comps[2] << comps[3] << std::nouppercase << std::dec;
+const auto comps = (color._data * 255.0f).clamped(0.0f, 255.0f).round(); // or explicit cast
+s << '#' << std::uppercase << std::hex
+  << std::setw(2) << std::setfill('0') << uint32_t(comps[0])
+  << std::setw(2) << std::setfill('0') << uint32_t(comps[1])
+  << std::setw(2) << std::setfill('0') << uint32_t(comps[2])
+  << std::setw(2) << std::setfill('0') << uint32_t(comps[3])
+  << std::dec << std::nouppercase;

Requires <iomanip> and a round()/clamped helper, but produces predictable 8-digit ARGB output.

Committable suggestion skipped: line range outside the PR's diff.

Comment on lines +44 to +48
ColorDerived &operator+=(const ColorDerived &other) noexcept {
static_cast<ColorDerived*>(this)->_data += other._data;
return *(ColorDerived*)this;
}

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Unsafe pointer casts & missing const qualification in operator+=
static_cast<ColorDerived*>(this)->_data += other._data; forces a mutable pointer even when *this is const.
Use a reference, keep qualifiers, and avoid C-style casts:

-ColorDerived &operator+=(const ColorDerived &other) noexcept {
-  static_cast<ColorDerived*>(this)->_data += other._data;
-  return *(ColorDerived*)this;
-}
+ColorDerived &operator+=(const ColorDerived &other) noexcept {
+  auto &self = static_cast<ColorDerived &>(*this);
+  self._data += other._data;
+  return self;
+}

This addresses UB and removes the noisy casts.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
ColorDerived &operator+=(const ColorDerived &other) noexcept {
static_cast<ColorDerived*>(this)->_data += other._data;
return *(ColorDerived*)this;
}
ColorDerived &operator+=(const ColorDerived &other) noexcept {
auto &self = static_cast<ColorDerived &>(*this);
self._data += other._data;
return self;
}

Comment on lines +185 to 190
mr::RGBAColor c1; // same as Vec4f(0, 0, 0, 0)
mr::RGBAColor c2 = mr::Color(0.3, 0.47, 0.8); // == (0.3, 0.47, 0.8, 1.0)
mr::RGBAColor c3 = mr::Color(76, 119, 204, 255); // == (0.2980392156862745, 0.4666666666666667, 0.8, 1.0)
mr::RGBAColor c4 = mr::Color(0x4C'77'CC'FF); // == (0.2980392156862745, 0.4666666666666667, 0.8, 1.0)
mr::RGBAColor c5 = 0x4C'77'CC'FF_rgba; // == (0.2980392156862745, 0.4666666666666667, 0.8, 1.0)
```
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick (assertive)

Examples still reference the removed mr::Color type
The refactor renamed the type to mr::RGBAColor, yet the docs mix both names:

-mr::RGBAColor c2 = mr::Color(0.3, 0.47, 0.8);
-mr::RGBAColor c3 = mr::Color(76, 119, 204, 255);
-mr::RGBAColor c4 = mr::Color(0x4C'77'CC'FF);
+mr::RGBAColor c2{0.3, 0.47, 0.8};
+mr::RGBAColor c3{76, 119, 204, 255};
+mr::RGBAColor c4{0x4C'77'CC'FF};

Do the same for the arithmetic example further down (mr::Color(1.0, …)mr::RGBAColor{…}).
Keeping the docs in sync prevents confusion for consumers.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
mr::RGBAColor c1; // same as Vec4f(0, 0, 0, 0)
mr::RGBAColor c2 = mr::Color(0.3, 0.47, 0.8); // == (0.3, 0.47, 0.8, 1.0)
mr::RGBAColor c3 = mr::Color(76, 119, 204, 255); // == (0.2980392156862745, 0.4666666666666667, 0.8, 1.0)
mr::RGBAColor c4 = mr::Color(0x4C'77'CC'FF); // == (0.2980392156862745, 0.4666666666666667, 0.8, 1.0)
mr::RGBAColor c5 = 0x4C'77'CC'FF_rgba; // == (0.2980392156862745, 0.4666666666666667, 0.8, 1.0)
```
mr::RGBAColor c1; // same as Vec4f(0, 0, 0, 0)
mr::RGBAColor c2{0.3, 0.47, 0.8}; // == (0.3, 0.47, 0.8, 1.0)
mr::RGBAColor c3{76, 119, 204, 255}; // == (0.2980392156862745, 0.4666666666666667, 0.8, 1.0)
mr::RGBAColor c4{0x4C'77'CC'FF}; // == (0.2980392156862745, 0.4666666666666667, 0.8, 1.0)
mr::RGBAColor c5 = 0x4C'77'CC'FF_rgba; // == (0.2980392156862745, 0.4666666666666667, 0.8, 1.0)
🧰 Tools
🪛 markdownlint-cli2 (0.17.2)

190-190: Fenced code blocks should be surrounded by blank lines
null

(MD031, blanks-around-fences)

Comment on lines +37 to 44
const mr::RGBAColor expected1{0.3, 0.47, 0.8, 1.0};
EXPECT_EQ(mr::RGBAColor(0.3, 0.47, 0.8), expected1);
EXPECT_EQ(mr::RGBAColor(mr::Vec4f(0.3, 0.47, 0.8, 1)), expected1);

const mr::Color expected2{0.2980392156862745, 0.4666666666666667, 0.8, 1.0};
EXPECT_EQ(mr::Color(76, 119, 204, 255), expected2);
EXPECT_EQ(mr::Color(0x4C'77'CC'FF), expected2);
const mr::RGBAColor expected2{0.2980392156862745, 0.4666666666666667, 0.8, 1.0};
EXPECT_EQ(mr::RGBAColor(76, 119, 204, 255), expected2);
EXPECT_EQ(mr::RGBAColor(0x4C'77'CC'FF), expected2);
EXPECT_EQ(0x4C'77'CC'FF_rgba, expected2);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick (assertive)

Consider using EXPECT_FLOAT_EQ for floating‐point constructor tests

The constructors accepting floats and vectors compare RGBAColor instances via EXPECT_EQ, which relies on operator==. If operator== uses exact float comparison, tiny rounding differences could cause spurious failures. Consider switching to EXPECT_TRUE(equal(...)) or EXPECT_FLOAT_EQ on each component for these tests to tolerate floating‐point precision.

Copy link
@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

♻️ Duplicate comments (5)
include/mr-math/color.hpp (5)

17-20: Unconditional static_assert still prevents all instantiations

This exact issue was raised in the previous review: the primary template aborts every build even when a matching specialization exists. Make the assertion dependent on the template parameters.


27-38: deducing this is the cause of the current CI syntax errors

GCC 13 / Clang 17 (our tool-chain) do not support the P0847 “deducing-this” syntax yet. Every use (get, the two conversion operators) fails to compile (self/other/eps undeclared).


44-48: operator+= still does UB-prone pointer casts

Casting away const and going through a pointer was already flagged; the unsafe pattern is unchanged.


49-55: Equality helpers repeat the unsupported “deducing-this” style

Again identical to the previously reported issue; the build stops here.


57-62: operator<< prints wrong hex & silently truncates ‑ nothing changed

The earlier suggestions (use std::setw, explicit rounding, avoid << std::hex on floats) are still applicable.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: ASSERTIVE
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between ae0812d and c4ddbf9.

📒 Files selected for processing (1)
  • include/mr-math/color.hpp (3 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
include/mr-math/color.hpp (4)
include/mr-math/matr.hpp (8)
  • nodiscard (169-171)
  • nodiscard (173-175)
  • nodiscard (178-191)
  • _data (321-324)
  • _data (383-386)
  • _data (455-458)
  • i (169-169)
  • i (173-173)
include/mr-math/operators.hpp (2)
  • s (133-141)
  • s (134-134)
include/mr-math/def.hpp (6)
  • epsilon (35-37)
  • epsilon (35-35)
  • value (93-93)
  • value (93-93)
  • value (116-116)
  • value (116-116)
include/mr-math/vec.hpp (7)
  • l (165-169)
  • l (172-175)
  • i (84-84)
  • i (84-84)
  • i (95-95)
  • y (86-86)
  • y (86-86)
🪛 GitHub Actions: Benchmarks
include/mr-math/color.hpp

[error] 29-54: Syntax errors due to incorrect use of 'this' keyword in method parameters and undeclared identifiers 'self', 'other', and 'eps'.


[error] 371-371: No matching function for call to 'mr::math::Vec<float, 3>::Vec(mr::math::RGBAColor&)'. Invalid static_cast from RGBAColor to Vec3f.

⏰ Context from checks skipped due to timeout of 90000ms (3)
  • GitHub Check: build (windows-latest, Release, cl)
  • GitHub Check: build (windows-latest, Debug, cl)
  • GitHub Check: build (ubuntu-24.04, Release, clang)

Comment on lines +138 to +156
struct HSLAColor {
friend class ColorBase<HSLAColor>;

private:
Vec4f _data;

public:
using ValueT = float;

HSLAColor() = default;

template <std::floating_point T>
HSLAColor(T h, T s, T l, T a = 1) noexcept
: _data(h, s, l, a) {}

HSLAColor(Vec4f hsla) noexcept
: _data(hsla) {}

template <std::integral T>
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

All new colour structs forget to inherit from ColorBase

HSLAColor, HSVAColor, CMYKColor, and OKLABColor only friend the base but do not derive from it, so they do not get:

  • operator+, operator+=
  • equality helpers
  • structured-binding support
  • stream output
    …and any future generic utilities.

This is a functional break (e.g. HSLAColor{} can’t be streamed, added, or compared).

Apply the pattern already used for RGBAColor:

-struct HSLAColor {
-  friend class ColorBase<HSLAColor>;
+struct HSLAColor : ColorBase<HSLAColor> {
+  friend class ColorBase<HSLAColor>;

Repeat the same one-liner change for HSVAColor, CMYKColor, and OKLABColor.

Also applies to: 196-214, 254-272, 312-330

Comment on lines +370 to +381
inline OKLABColor convert<RGBAColor, OKLABColor>(RGBAColor rgba) {
auto v = static_cast<Vec3f>(rgba) * Matr3f {
Matr3f::RowT {0.4122214708f, 0.2119034982f, 0.0883024619f},
Matr3f::RowT {0.5363325363f, 0.6806995451f, 0.2817188376f},
Matr3f::RowT {0.0514459929f, 0.1073969566f, 0.6299787005f},
};

// TODO: rewrite using mr-math adaptation of <cmath>
float l_ = cbrtf(v[0]);
float m_ = cbrtf(v[1]);
float s_ = cbrtf(v[2]);

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

❓ Verification inconclusive

convert<RGBAColor, OKLABColor> currently fails to compile

  1. static_cast<Vec3f>(rgba) relies on the (unsupported) conversion operator discussed above, causing the pipeline error at line 371.
  2. The multiplication order Vec3f * Matr3f may be wrong depending on how the library defines operator*. Most math libraries expect Matr * Vec, not the other way around.
-  auto v = static_cast<Vec3f>(rgba) * Matr3f{
+  Vec3f rgb = {rgba.r(), rgba.g(), rgba.b()};
+  auto v = Matr3f{
     {0.4122214708f, 0.2119034982f, 0.0883024619f},
     {0.5363325363f, 0.6806995451f, 0.2817188376f},
     {0.0514459929f, 0.1073969566f, 0.6299787005f},
-  };
+  } * rgb;              // matrix-vector product

After the earlier conversion-operator rewrite, you could switch back to static_cast<Vec3f>(rgba) if desired.

Please confirm the actual operator* signature; run:


🏁 Script executed:

#!/bin/bash
# Show all operator* overloads involving Matr3f and Vec3f
rg -A2 -e 'operator\*.*Matr3f' -t cpp
rg -A2 -e 'operator\*.*Vec3f'  -t cpp

Length of output: 78


I couldn’t find any operator* overloads involving Matr3f or Vec3f with the previous search. Let’s run a broader grep to catch definitions in both headers and sources:


🏁 Script executed:

#!/bin/bash
# Look for any operator* overloads that mention Matr3f or Vec3f
rg -n 'operator\*' -t cpp -t hpp | grep -E 'Matr3f|Vec3f'

Length of output: 87


Let’s locate Matr3f’s definition and then search for any operator* there and globally:


🏁 Script executed:

#!/bin/bash
# 1) Find the file that defines Matr3f
def_file=$(rg -l 'struct Matr3f' | head -n1)
echo "Matr3f defined in: $def_file"

# 2) Search for operator* in that file
if [ -n "$def_file" ]; then
  echo "—— operator* in $def_file ——"
  grep -n 'operator\*' "$def_file" || echo "none"
else
  echo "Matr3f definition file not found"
fi

# 3) Search globally for any operator* involving Matr3f or Vec3f
echo "—— global operator* occurrences involving Matr3f/Vec3f ——"
rg -n 'operator\*' | grep -E 'Matr3f|Vec3f' || echo "none"

Length of output: 376


Please verify the RGBA→Vec3f conversion and the matrix‐vector multiplication signature

  • The use of static_cast<Vec3f>(rgba) fails because there’s no supported conversion operator from RGBAColor to Vec3f.
  • We could not locate any operator* overload for Matr3f or Vec3f in the codebase. Confirm whether your math library defines
    • Matr3f operator*(Matr3f, Vec3f)
    • Vec3f operator*(Vec3f, Matr3f)
      and adjust the multiplication order accordingly.
-  auto v = static_cast<Vec3f>(rgba) * Matr3f{
+  Vec3f rgb{ rgba.r(), rgba.g(), rgba.b() };
+  auto v = Matr3f{
     {0.4122214708f, 0.2119034982f, 0.0883024619f},
     {0.5363325363f, 0.6806995451f, 0.2817188376f},
     {0.0514459929f, 0.1073969566f, 0.6299787005f},
-  };
+  } * rgb;  // matrix * vector

Once you’ve confirmed or added the proper operator*, you may revert to static_cast<Vec3f>(rgba) if you prefer.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
inline OKLABColor convert<RGBAColor, OKLABColor>(RGBAColor rgba) {
auto v = static_cast<Vec3f>(rgba) * Matr3f {
Matr3f::RowT {0.4122214708f, 0.2119034982f, 0.0883024619f},
Matr3f::RowT {0.5363325363f, 0.6806995451f, 0.2817188376f},
Matr3f::RowT {0.0514459929f, 0.1073969566f, 0.6299787005f},
};
// TODO: rewrite using mr-math adaptation of <cmath>
float l_ = cbrtf(v[0]);
float m_ = cbrtf(v[1]);
float s_ = cbrtf(v[2]);
inline OKLABColor convert<RGBAColor, OKLABColor>(RGBAColor rgba) {
// extract RGB components explicitly
Vec3f rgb{ rgba.r(), rgba.g(), rgba.b() };
// perform matrix–vector multiply (ensure Matr3f * Vec3f exists)
auto v = Matr3f{
{0.4122214708f, 0.2119034982f, 0.0883024619f},
{0.5363325363f, 0.6806995451f, 0.2817188376f},
{0.0514459929f, 0.1073969566f, 0.6299787005f},
} * rgb;
// TODO: rewrite using mr-math adaptation of <cmath>
float l_ = cbrtf(v[0]);
float m_ = cbrtf(v[1]);
float s_ = cbrtf(v[2]);
🧰 Tools
🪛 GitHub Actions: Benchmarks

[error] 371-371: No matching function for call to 'mr::math::Vec<float, 3>::Vec(mr::math::RGBAColor&)'. Invalid static_cast from RGBAColor to Vec3f.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant
0