11#define NOVASVG_MAX(a, b) ((a) > (b) ? (a) : (b))
14#define NOVASVG_MIN(a, b) ((a) < (b) ? (a) : (b))
114 explicit Color(uint8_t r = 0, uint8_t g = 0, uint8_t b = 0, uint8_t a = 255)
115 : r(r), g(g), b(b), a(a) {}
170 template <
typename OtherColor,
171 typename =
decltype(std::declval<const OtherColor&>().red()),
172 typename =
decltype(std::declval<const OtherColor&>().green()),
173 typename =
decltype(std::declval<const OtherColor&>().blue()),
174 typename =
decltype(std::declval<const OtherColor&>().alpha())>
175 constexpr Color(
const OtherColor& other)
176 : r(
static_cast<uint8_t
>(other.red())),
177 g(
static_cast<uint8_t
>(other.green())),
178 b(
static_cast<uint8_t
>(other.blue())),
179 a(
static_cast<uint8_t
>(other.alpha())) {}
202 template <
typename OtherColor>
203 constexpr OtherColor
as()
const {
204 return OtherColor(r, g, b, a);
213 uint8_t
getR()
const {
return r; }
219 uint8_t
getG()
const {
return g; }
225 uint8_t
getB()
const {
return b; }
232 uint8_t
getA()
const {
return a; }
265 float getRF()
const {
return r / 255.f; }
267 float getGF()
const {
return g / 255.f; }
269 float getBF()
const {
return b / 255.f; }
271 float getAF()
const {
return a / 255.f; }
283 return Color(r, g, b,
static_cast<uint8_t
>(a * opacity));
309 return (
static_cast<uint32_t
>(r) << 24) |
310 (
static_cast<uint32_t
>(g) << 16) |
311 (
static_cast<uint32_t
>(b) << 8) |
312 static_cast<uint32_t
>(a);
336 return lhs.r == rhs.r &&
363 return !(lhs == rhs);
384 operator std::string()
const {
return toString(); }
401 std::ostringstream oss;
402 oss <<
"Color(r=" <<
static_cast<int>(r)
403 <<
", g=" <<
static_cast<int>(g)
404 <<
", b=" <<
static_cast<int>(b)
405 <<
", a=" <<
static_cast<int>(a) <<
")";
450 return blend(other, 50);
472 double p = pct / 100.0;
476 static_cast<uint8_t
>(std::ceil(r * q + other.r * p)),
477 static_cast<uint8_t
>(std::ceil(g * q + other.g * p)),
478 static_cast<uint8_t
>(std::ceil(b * q + other.b * p)),
479 static_cast<uint8_t
>(std::ceil(a * q + other.a * p))
521 throw std::invalid_argument(
"Empty string provided");
523 bool is_hex = std::all_of(str.begin(), str.end(), [](
unsigned char c) { return std::isxdigit(c); });
545 static Color rgba(uint8_t r, uint8_t g, uint8_t b, uint8_t a = 255) {
546 return Color(r, g, b, a);
576 if (hex_code.empty() || hex_code[0] !=
'#') {
577 throw std::invalid_argument(
"Hex color string must start with '#'");
580 std::string hex_body = hex_code.substr(1);
582 if (hex_body.length() != 3 && hex_body.length() != 4 &&
583 hex_body.length() != 6 && hex_body.length() != 8) {
584 throw std::invalid_argument(
"Invalid hex color format");
588 if (hex_body.length() == 3) {
589 hex_body = std::string() + hex_body[0] + hex_body[0]
590 + hex_body[1] + hex_body[1]
591 + hex_body[2] + hex_body[2];
594 else if (hex_body.length() == 4) {
595 hex_body = std::string() + hex_body[0] + hex_body[0]
596 + hex_body[1] + hex_body[1]
597 + hex_body[2] + hex_body[2]
598 + hex_body[3] + hex_body[3];
601 uint8_t r =
static_cast<uint8_t
>(std::stoi(hex_body.substr(0, 2),
nullptr, 16));
602 uint8_t g =
static_cast<uint8_t
>(std::stoi(hex_body.substr(2, 2),
nullptr, 16));
603 uint8_t b =
static_cast<uint8_t
>(std::stoi(hex_body.substr(4, 2),
nullptr, 16));
604 uint8_t a = (hex_body.length() == 8)
605 ?
static_cast<uint8_t
>(std::stoi(hex_body.substr(6, 2),
nullptr, 16))
608 return Color(r, g, b, a);
637 std::string name_lower = name;
638 for (
char& c : name_lower) c =
static_cast<char>(std::tolower(c));
640 if (name_lower ==
"black")
return black();
641 if (name_lower ==
"white")
return white();
642 if (name_lower ==
"red")
return red();
643 if (name_lower ==
"green")
return green();
644 if (name_lower ==
"lime")
return lime();
645 if (name_lower ==
"blue")
return blue();
646 if (name_lower ==
"yellow")
return yellow();
647 if (name_lower ==
"cyan")
return cyan();
648 if (name_lower ==
"magenta")
return magenta();
649 if (name_lower ==
"silver")
return silver();
650 if (name_lower ==
"gray" || name_lower ==
"grey")
return gray();
651 if (name_lower ==
"maroon")
return maroon();
652 if (name_lower ==
"olive")
return olive();
653 if (name_lower ==
"purple")
return purple();
654 if (name_lower ==
"teal")
return teal();
655 if (name_lower ==
"navy")
return navy();
656 if (name_lower ==
"orange")
return orange();
657 if (name_lower ==
"pink")
return pink();
658 if (name_lower ==
"brown")
return brown();
659 if (name_lower ==
"transparent")
return transparent();
661 throw std::invalid_argument(
"Unknown color name: '" + name +
"'");
693 uint8_t r =
static_cast<uint8_t
>((
value >> 24) & 0xFF);
694 uint8_t g =
static_cast<uint8_t
>((
value >> 16) & 0xFF);
695 uint8_t b =
static_cast<uint8_t
>((
value >> 8) & 0xFF);
696 uint8_t a =
static_cast<uint8_t
>(
value & 0xFF);
697 return Color(r, g, b, a);
945 os <<
color.toString();
960 os << static_cast<Color>(
blend);
Represents a color in RGBA format (0xRRGGBBAA).
float getGF() const
Green component as a 0..1 float.
static Color purple()
Returns the color purple (128, 0, 128, 255).
static Color rgba(uint8_t r, uint8_t g, uint8_t b, uint8_t a=255)
Create a color from RGBA components.
static Color brown()
Returns the color brown (165, 42, 42, 255).
void setB(uint8_t value)
Set the blue component of the color.
static Color magenta()
Returns the color magenta (255, 0, 255, 255).
Color(uint8_t r=0, uint8_t g=0, uint8_t b=0, uint8_t a=255)
Construct a new Color object from RGBA components.
static Color transparent()
Returns the color transparent (0, 0, 0, 0).
void setA(uint8_t value)
Set the alpha component of the color.
float getBF() const
Blue component as a 0..1 float.
static Color olive()
Returns the color olive (128, 128, 0, 255).
static Color yellow()
Returns the color yellow (255, 255, 0, 255).
static Color maroon()
Returns the color maroon (128, 0, 0, 255).
static Color pink()
Returns the color pink (255, 192, 203, 255).
uint8_t getB() const
Get the blue component of the color.
friend bool operator!=(const Color &lhs, const Color &rhs)
Inequality comparison operator.
static Color navy()
Returns the color navy (0, 0, 128, 255).
static Color orange()
Returns the color orange (255, 165, 0, 255).
static Color black()
Returns the color black (0, 0, 0, 255).
Color colorWithAlpha(float opacity) const
This color with its alpha scaled by opacity (0..1).
static Color cyan()
Returns the color cyan (0, 255, 255, 255).
static Color fromName(const std::string &name)
Create a color from a standard CSS color name.
Color blend(const Color &other, int percentage) const
Blend this color with another color by a specific percentage.
static Color blue()
Returns the color blue (0, 0, 255, 255).
constexpr OtherColor as() const
Convert this Color to any "color-like" type constructible from four (r, g, b, a) components.
uint8_t getR() const
Get the red component of the color.
static Color fromHash(const std::string &hex_code)
Create a color from a hexadecimal string.
static Color lime()
Returns the color lime (0, 255, 0, 255).
bool isOpaque() const
True if fully opaque (alpha == 255).
void setR(uint8_t value)
Set the red component of the color.
static Color red()
Returns the color red (255, 0, 0, 255).
friend bool operator==(const Color &lhs, const Color &rhs)
Equality comparison operator.
static Color fromValue(uint32_t value)
Create a color from a 32-bit integer value.
uint32_t value() const
Convert the color to a 32-bit unsigned integer.
static Color gray()
Returns the color gray (128, 128, 128, 255).
bool isVisible() const
True if not fully transparent (alpha > 0).
static const Color Transparent
float getRF() const
Red component as a 0..1 float.
Color(const std::string &str)
Construct a new Color object from a string.
std::string toString() const
Convert the color to a human-readable string.
Color operator|(const Color &other) const
Blend this color with another color (50% each).
static Color green()
Returns the color green (0, 128, 0, 255).
uint8_t getA() const
Get the alpha component of the color.
static Color fromString(const std::string &str)
Create a color from a string (auto-detects hex or name).
Color opaqueColor() const
This color with alpha forced to fully opaque (255).
float getAF() const
Alpha component as a 0..1 float.
void setG(uint8_t value)
Set the green component of the color.
constexpr Color(const OtherColor &other)
Construct a Color from any "color-like" type.
static Color teal()
Returns the color teal (0, 128, 128, 255).
static Color silver()
Returns the color silver (192, 192, 192, 255).
ColorBlend operator|(int percentage) const
Blend this color with transparent by a percentage.
uint8_t getG() const
Get the green component of the color.
static Color white()
Returns the color white (255, 255, 255, 255).
#define NOVASVG_MAX(a, b)
#define NOVASVG_MIN(a, b)
String literal operators for creating Color instances.
NOVASVG_INLINE void blend(canvas_t *canvas, const span_buffer_t *span_buffer)
Main namespace for the NovaSVG library.
std::ostream & operator<<(std::ostream &os, const Color &color)
Stream output operator for Color.
Lightweight helper class for color blending operations.
ColorBlend(const Color &base, int percent)
Construct a ColorBlend from a base color and percentage.
ColorBlend operator|(const ColorBlend &other) const
Chain multiple blend operations.
int percentage
Percentage for the blend operation (0-100)
Color baseColor
The base color being blended.
Color operator|(const Color &other) const
Finalize the blend with a specific color.
Represents a color with red, green, blue, and alpha components.