API Color - shmellyorc/Box GitHub Wiki
Color
Namespace: Box.Graphics
Description:
Represents a color in the RGB (Red, Green, Blue) color space with an alpha (transparency) channel. The Color
struct provides constructors for various input types (ints, floats, hex, copy), properties to access and clamp individual RGBA components, operator overloads for arithmetic, and utility methods for blending, lightening, darkening, and brightness checks.
Constructors
public Color(int red, int green, int blue, int alpha)
Initializes a new instance with specified red, green, blue, and alpha components (0â255).
public Color(int red, int green, int blue)
Initializes a new instance with specified RGB components (0â255) and default alpha 255.
public Color(float red, float green, float blue, float alpha)
Initializes a new instance from normalized float values (0.0fâ1.0f) for RGBA.
public Color(float red, float green, float blue)
Initializes a new instance from normalized float RGB values and default alpha 1.0f.
public Color(BoxColor color)
Initializes a copy of another Color
instance.
public Color(string hex)
Initializes a new instance by parsing a hexadecimal string (#RRGGBB, #AARRGGBB, or #RGB).
Properties
Property | Type | Description |
---|---|---|
int Red |
Gets or sets the red component (0â255). | |
int Green |
Gets or sets the green component (0â255). | |
int Blue |
Gets or sets the blue component (0â255). | |
int Alpha |
Gets or sets the alpha (transparency) (0â255). |
Methods
Method Signature | Description |
---|---|
static BoxColor operator +(BoxColor left, BoxColor right) |
Adds two colors component-wise. |
static BoxColor operator -(BoxColor left, BoxColor right) |
Subtracts one color from another component-wise. |
static BoxColor operator *(BoxColor left, float right) |
Multiplies each component by a scalar (0.0â1.0). |
static BoxColor operator /(BoxColor left, float right) |
Divides each component by a scalar (clamped 0.0â1.0). |
static BoxColor operator /(BoxColor left, int right) |
Divides each component by an integer. |
static bool operator ==(BoxColor left, BoxColor right) |
Checks equality of two colors. |
static bool operator !=(BoxColor left, BoxColor right) |
Checks inequality of two colors. |
BoxColor BlendColors(BoxColor other, double ratio) |
Blends this color with another by ratio (0.0â1.0). |
static BoxColor BlendColors(BoxColor color1, BoxColor color2, double ratio) |
Blends two colors by ratio (0.0â1.0). |
void Lighten(int amount) |
Increases RGB components by amount (clamped to 255). |
static void Lighten(BoxColor color, int amount) |
Lightens the given color (RGB only). |
void LightenWithAlpha(int amount) |
Lightens RGBA components by amount (clamped to 255). |
static void LightenWithAlpha(BoxColor color, int amount) |
Lightens the given color including alpha. |
void Darken(int amount) |
Decreases RGB components by amount (clamped to 0). |
static void Darken(BoxColor color, int amount) |
Darkens the given color (RGB only). |
void DarkenWithAlpha(int amount) |
Decreases RGBA components by amount (clamped to 0). |
static void DarkenWithAlpha(BoxColor color, int amount) |
Darkens the given color including alpha. |
bool IsDark(int brightnessThreshold = 128) |
Returns true if average RGB brightness < threshold. |
static bool IsDark(BoxColor color, int brightnessThreshold = 128) |
Static brightness check (dark). |
bool IsLight(int brightnessThreshold = 128) |
Returns true if average RGB brightness ⼠threshold. |
static bool IsLight(BoxColor color, int brightnessThreshold = 128) |
Static brightness check (light). |
bool Equals(BoxColor other) |
Compares this color to another for equality. |
override bool Equals(object? obj) |
Compares this color to an object for equality. |
override int GetHashCode() |
Returns a hash code based on RGBA components. |
override string ToString() |
Returns a string "R, G, B, A" representation. |
Examples
// From integer components
over = new Color(255, 128, 0);
// From floats
var semiTransparent = new Color(1.0f, 0.0f, 0.0f, 0.5f);
// From hex
var teal = new Color("#008080");
// Blend two colors
var mid = Color.BlendColors(Color.Red, Color.Blue, 0.5);
// Lighten and darken
mid.Lighten(30);
var darker = mid;
Color.Darken(darker, 20);
// Check brightness
bool isDark = mid.IsDark();
// String repr
Console.WriteLine(mid.ToString()); // "