Color representation
Energy.Base.Color unifies all RGB/HSL/RAL helpers exposed by the core library. The struct stores 8-bit A/R/G/B channels and now includes conversion helpers for HSL parsing, RAL catalog matching, and alpha flattening.
FlattenAlpha
Remove transparency by blending any single channel against a white background.
double Energy.Base.Color.FlattenAlpha(byte channel, byte alpha)
Returns a
doubleto avoid rounding errors.Used by the RAL matcher to normalize semi-transparent pixels before color-space conversion.
HexToColor
Convert HTML/CSS color literals into the Energy.Base.Color struct.
Energy.Base.Color Energy.Base.Color.HexToColor(string text)
Accepts short (
#abc,abc) and long (#abcdef,abcdef) notations.Returns a fully opaque color when parsing succeeds; otherwise returns
0.Under the hood the method expands single-digit components before calling
Energy.Base.Hex.HexToArray.
ColorToHtml
Serialize a color back into a lowercase HTML hex string.
string Energy.Base.Color.ColorToHtml(Energy.Base.Color color)
Always emits
#rrggbb.Useful for logging, diagnostics, or generating CSS values from the struct.
ColorTransition
Blend two colors using a percentage between 0.0 and 1.0 (or 0-100 after normalization).
Energy.Base.Color Energy.Base.Color.ColorTransition(Energy.Base.Color from, Energy.Base.Color to, double percentage)
Handles negative percentages by swapping inputs, and percentages > 1 by treating them as percentages.
Interpolates each ARGB channel separately, returning a new
Energy.Base.Color.
HSL helpers
HSL parsing is grouped inside the nested Energy.Base.Color.HSL class.
bool Energy.Base.Color.HSL.IsValidHsl(string input)
Energy.Base.Color Energy.Base.Color.HSL.HslToColor(string input)
Accepts free-form strings such as
(120°, 100%, 25%),120,100,25, orH,S,L,Awith optional percent symbols.Validates ranges (H 0-360, S/L 0-100, alpha 0-1 or 0-100%).
HslToColorthrowsFormatExceptionwhen validation fails.Internally uses the
Hue2Rgbhelper to linearize the components before converting to 0-255 bytes.
RAL helpers
The nested Energy.Base.Color.RAL class provides bidirectional conversion between RGBA samples and the official RAL catalogue.
string Energy.Base.Color.RAL.ColorToRal(Energy.Base.Color rgba)
Energy.Base.Color Energy.Base.Color.RAL.RalToColor(string code)
Features:
Alpha flattening – channels are blended with white via
FlattenAlphabefore converting into CIELAB space.CIELAB matching – colors are transformed into LAB (D65) and compared with stored palette entries using DeltaE.
Flexible RAL input –
RalToColoraccepts values such as"RAL 7016","ral7016","RaL-7016", or just"7016". Non-digit characters outside whitespace,R/A/L, dashes, or underscores will be rejected.Full palette coverage – the embedded palette stores the RGB and LAB coordinates for common industrial colors (7016 Anthracite grey, 9005 Jet black, 9010 Pure white, 9006 White aluminium, 7035 Light grey, 6005 Moss green, 3020 Traffic red, 5010 Gentian blue, 8017 Chocolate brown, 1021 Rape yellow).
Example usage:
Energy.Base.Color color = Energy.Base.Color.HSL.HslToColor("(9°, 100%, 64%, 50%)");
string ral = Energy.Base.Color.RAL.ColorToRal(color);
Energy.Base.Color paint = Energy.Base.Color.RAL.RalToColor("RAL 3020");
Unit tests in Energy.Core.Test/Base/Color.cs exercise both helpers to guarantee every palette entry round-trips correctly and that flexible input formats remain supported.