VisuTwin Canvas
C++ 3D Engine — Metal Backend
Loading...
Searching...
No Matches
color.h
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2// Copyright 2025-2026 Arnis Lektauers
3#pragma once
4
5#include <string>
6#include <vector>
7#include <array>
8
9namespace visutwin::canvas
10{
18 struct Color {
19 float r;
20
21 float g;
22
23 float b;
24
25 float a;
26
27 explicit Color(const float r = 0.0f, const float g = 0.0f, const float b = 0.0f, const float a = 1.0f) : r(r), g(g), b(b), a(a) {}
28
33 explicit Color(const std::vector<float>& arr);
34
40 Color(const float* arr, size_t size);
41
45 Color(const Color& other) = default;
46
47 Color& operator=(const Color& other) = default;
48
53 [[nodiscard]] Color clone() const;
54
60 Color& copy(const Color& rhs);
61
67 [[nodiscard]] bool equals(const Color& rhs) const;
68
77 Color& set(float r, float g, float b, float a = 1.0f);
78
88 Color& lerp(const Color& lhs, const Color& rhs, float alpha);
89
96 Color& linear(const Color* src = nullptr);
97
104 Color& gamma(const Color* src = nullptr);
105
111 Color& mulScalar(float scalar);
112
120 Color& fromString(const std::string& hex);
121
129 Color& fromArray(const std::vector<float>& arr, size_t offset = 0);
130
139 Color& fromArray(const float* arr, size_t size, size_t offset = 0);
140
149 [[nodiscard]] std::string toString(bool alpha = false, bool asArray = false) const;
150
159 [[nodiscard]] std::vector<float> toArray(std::vector<float> arr = {}, size_t offset = 0, bool alpha = true) const;
160
169 void toArray(float* arr, size_t size, size_t offset = 0, bool alpha = true) const;
170
171 bool operator==(const Color& other) const;
172 bool operator!=(const Color& other) const;
173
174 static const Color BLACK;
175 static const Color BLUE;
176 static const Color CYAN;
177 static const Color GRAY;
178 static const Color GREEN;
179 static const Color MAGENTA;
180 static const Color RED;
181 static const Color WHITE;
182 static const Color YELLOW;
183
184 private:
190 static std::array<uint8_t, 3> intToBytes24(uint32_t i);
191
197 static std::array<uint8_t, 4> intToBytes32(uint32_t i);
198
206 static float clamp(float value, float min, float max);
207 };
208}
RGBA color with floating-point components in [0, 1].
Definition color.h:18
Color(const float r=0.0f, const float g=0.0f, const float b=0.0f, const float a=1.0f)
Definition color.h:27
Color & lerp(const Color &lhs, const Color &rhs, float alpha)
Definition color.cpp:80
static const Color BLACK
Definition color.h:174
std::string toString(bool alpha=false, bool asArray=false) const
Definition color.cpp:167
static const Color YELLOW
Definition color.h:182
Color & mulScalar(float scalar)
Definition color.cpp:109
Color(const Color &other)=default
static const Color MAGENTA
Definition color.h:179
Color & copy(const Color &rhs)
Definition color.cpp:57
static const Color WHITE
Definition color.h:181
bool operator!=(const Color &other) const
Definition color.cpp:246
bool operator==(const Color &other) const
Definition color.cpp:241
Color clone() const
Definition color.cpp:52
static const Color BLUE
Definition color.h:175
bool equals(const Color &rhs) const
Definition color.cpp:66
static const Color CYAN
Definition color.h:176
Color & gamma(const Color *src=nullptr)
Definition color.cpp:99
static const Color GREEN
Definition color.h:178
std::vector< float > toArray(std::vector< float > arr={}, size_t offset=0, bool alpha=true) const
Definition color.cpp:202
Color & linear(const Color *src=nullptr)
Definition color.cpp:89
static const Color RED
Definition color.h:180
static const Color GRAY
Definition color.h:177
Color & operator=(const Color &other)=default
Color & fromString(const std::string &hex)
Definition color.cpp:117
Color & fromArray(const std::vector< float > &arr, size_t offset=0)
Definition color.cpp:149
Color & set(float r, float g, float b, float a=1.0f)
Definition color.cpp:71