ProtoTracer  1.0
Real-time 3D rendering and animation engine
Loading...
Searching...
No Matches
RGBColor565.h
Go to the documentation of this file.
1/**
2 * @file RGBColor.h
3 * @brief Defines the RGBColor class for managing and manipulating 16-bit RGB565 color values.
4 *
5 * The RGBColor class represents RGB colors in 16-bit format (RGB565) and provides methods for color manipulation,
6 * including scaling brightness, adding values, hue shifting, and interpolating between colors.
7 *
8 * @date 22/12/2024
9 * @version 1.0
10 * @author Coela Can't
11 */
12
13#pragma once
14
15#include "Math/Mathematics.h"
16#include "Math/Quaternion.h"
17#include "Math/Vector3D.h"
18
19/**
20 * @class RGBColor
21 * @brief Represents a 16-bit RGB color (RGB565) and provides methods for manipulation.
22 */
23class RGBColor {
24public:
25 uint16_t color = 0; ///< Encoded 16-bit RGB565 color value.
26
27 /**
28 * @brief Default constructor initializes the color to black (0, 0, 0).
29 */
31
32 /**
33 * @brief Constructor to initialize RGBColor with specified R, G, and B values.
34 * @param R Red component (0-255).
35 * @param G Green component (0-255).
36 * @param B Blue component (0-255).
37 */
39 R = (R >> 3) & 0x1F;
40 G = (G >> 2) & 0x3F;
41 B = (B >> 3) & 0x1F;
42
43 color = (R << 11) | (G << 5) | B;
44 }
45
46 /**
47 * @brief Copy constructor.
48 * @param rgbColor Reference to an existing RGBColor object.
49 */
51 this->R = rgbColor.R;
52 this->G = rgbColor.G;
53 this->B = rgbColor.B;
54 }
55
56 /**
57 * @brief Constructor to initialize RGBColor from a Vector3D.
58 * @param color Vector3D representing RGB values.
59 */
61 this->R = color.X;
62 this->G = color.Y;
63 this->B = color.Z;
64 }
65
66 /**
67 * @brief Sets the RGB values of the color.
68 * @param R Red component (0-255).
69 * @param G Green component (0-255).
70 * @param B Blue component (0-255).
71 */
73 this->R = R;
74 this->G = G;
75 this->B = B;
76 }
77
78 /**
79 * @brief Scales the brightness of the color to a maximum value.
80 * @param maxBrightness The maximum brightness value (0-255).
81 * @return A new RGBColor with scaled brightness.
82 */
83 RGBColor Scale(uint8_t maxBrightness) {
84 uint8_t sR = (uint8_t)R * (uint8_t)maxBrightness / 255;
85 uint8_t sG = (uint8_t)G * (uint8_t)maxBrightness / 255;
86 uint8_t sB = (uint8_t)B * (uint8_t)maxBrightness / 255;
87
88 return RGBColor(
92 );
93 }
94
95 /**
96 * @brief Adds a value to each RGB component of the color.
97 * @param value The value to add (0-255).
98 * @return A new RGBColor with modified values.
99 */
101 return RGBColor(
102 Mathematics::Constrain(R + value, 0, 255),
103 Mathematics::Constrain(G + value, 0, 255),
105 );
106 }
107
108 /**
109 * @brief Shifts the hue of the color by a specified angle in degrees.
110 * @param hueDeg The angle in degrees to shift the hue.
111 * @return A new RGBColor with the hue shifted.
112 */
114 float hueRad = hueDeg * Mathematics::MPI / 180.0f;
115 float hueRat = 0.5f * sinf(hueRad / 2.0f);
118
119 rgbVec = q.RotateVector(rgbVec).Constrain(0.0f, 255.0f);
120
121 return RGBColor(rgbVec.X, rgbVec.Y, rgbVec.Z);
122 }
123
124 /**
125 * @brief Interpolates between two colors based on a ratio.
126 * @param a The starting color.
127 * @param b The ending color.
128 * @param ratio A value between 0 and 1 representing the interpolation factor.
129 * @return The interpolated RGBColor.
130 */
131 static RGBColor InterpolateColors(RGBColor a, RGBColor b, float ratio) {
132 return RGBColor(
133 a.R * (1.0f - ratio) + b.R * ratio,
134 a.G * (1.0f - ratio) + b.G * ratio,
135 a.B * (1.0f - ratio) + b.B * ratio
136 );
137 }
138
139 /**
140 * @brief Converts the RGBColor to a string representation.
141 * @return A string in the format "[R, G, B]".
142 */
144 return "[" + String(R) + ", " + String(G) + ", " + String(B) + "]";
145 }
146};
Provides a collection of mathematical utility functions and constants.
Defines the Quaternion class for 3D rotations and transformations.
Defines a 3D vector and various related operations.
Implements a generic Kalman Filter for 1D data.
static const float MPI
Mathematical constant (3.14159265358979323846...).
Definition Mathematics.h:42
static T Constrain(T value, T minimum, T maximum)
Constrains a value between minimum and maximum.
A mathematical construct representing a rotation in 3D space.
Definition Quaternion.h:30
Represents an RGB color and provides methods for manipulation.
Definition RGBColor.h:23
String ToString()
Converts the RGBColor to a string representation.
static RGBColor InterpolateColors(RGBColor a, RGBColor b, float ratio)
Interpolates between two colors based on a ratio.
uint16_t color
Encoded 16-bit RGB565 color value.
Definition RGBColor565.h:25
RGBColor()
Default constructor initializes the color to black (0, 0, 0).
Definition RGBColor565.h:30
void SetColor(uint8_t R, uint8_t G, uint8_t B)
Sets the RGB values of the color.
Definition RGBColor565.h:72
uint8_t B
Blue component of the color (0-255).
Definition RGBColor.h:27
RGBColor(Vector3D color)
Constructor to initialize RGBColor from a Vector3D.
Definition RGBColor565.h:60
RGBColor(uint8_t R, uint8_t G, uint8_t B)
Constructor to initialize RGBColor with specified R, G, and B values.
Definition RGBColor565.h:38
uint8_t G
Green component of the color (0-255).
Definition RGBColor.h:26
RGBColor HueShift(float hueDeg)
Shifts the hue of the color by a specified angle in degrees.
RGBColor(const RGBColor &rgbColor)
Copy constructor.
Definition RGBColor565.h:50
RGBColor Add(uint8_t value)
Adds a value to each RGB component of the color.
uint8_t R
Red component of the color (0-255).
Definition RGBColor.h:25
RGBColor Scale(uint8_t maxBrightness)
Scales the brightness of the color to a maximum value.
Definition RGBColor565.h:83
Represents a 3D vector (X, Y, Z) and provides methods for vector arithmetic.
Definition Vector3D.h:26
float Z
The Z-component of the 3D vector.
Definition Vector3D.h:30
float X
The X-component of the 3D vector.
Definition Vector3D.h:28
float Y
The Y-component of the 3D vector.
Definition Vector3D.h:29