Class ColorUtil
Utility for handling color. Includes handle of RGB and HSL.
public static class ColorUtil
- Inheritance
-
ColorUtil
- Inherited Members
Properties
EnvDiscreteRgbSeed
public static int EnvDiscreteRgbSeed { get; set; }
Property Value
- int
The seed is applied to GetDiscreteRGB_Env(double, double).
ExceptionRed
Exception RGB. Also for nan value.
public static vec3d_t ExceptionRed { get; }
Property Value
Gray
Generate gray color. RGB=(0.2, 0.2, 0.2).
public static vec3d_t Gray { get; }
Property Value
NoValueGreen
No value RGB.
public static vec3d_t NoValueGreen { get; }
Property Value
NullDark
Generate dark RGB for null value. RGB=(0,0,0).
public static vec3d_t NullDark { get; }
Property Value
UndeterminedPurple
Undetermined RGB.
public static vec3d_t UndeterminedPurple { get; }
Property Value
Methods
FromDualHexString(string)
Parses an RRGGBB hexadecimal string (optionally prefixed with
#) back to an RGB color vector with channels in 0~1.
Inverse of ToDualHexString(vec3d_t) up to 8-bit rounding.
public static vec3d_t FromDualHexString(string hex)
Parameters
hexstringThe hexadecimal color string, e.g.
#3fa0c8.
Returns
- vec3d_t
RGB color vector.
GetDiscreteHue(int)
Get the HSL hue of the discrete color of seed:
the hue GetDiscreteRgb(int, double, double) hands to
HslToRgb(double, double, double).
The same seed always gives the same hue.
public static double GetDiscreteHue(int seed)
Parameters
seedintColor seed. Any integer, negative ones included.
Returns
- double
HSL hue in 0~1
Remarks
Each seed advances 0.618 of the usable hues, measured by perceived color difference rather than by degrees. Seeds 1, 2 or 4 apart are therefore far apart in hue; seeds 5, 8 or 13 apart are close.
The usable hues are 30° to 220° and 270° to 300°. Magenta through red-orange (300° to 30°) and deep blue through blue-violet (220° to 270°) never appear.
GetDiscreteRGBWithoutPurpleAndRed(int, double, double)
Get discrete RGB without purple and red color ranges.
public static vec3d_t GetDiscreteRGBWithoutPurpleAndRed(int seed, double saturation = 1, double light = 0.5)
Parameters
seedintColor seed. Determines the hue value.
saturationdoubleSaturation value of the color.
lightdoubleLight value of the color.
Returns
- vec3d_t
RGB color vector without purple and red ranges.
GetDiscreteRGB_Env(double, double)
Get discrete color using EnvDiscreteRgbSeed. The functionality is the same as GetDiscreteRgb(int, double, double). The function call makes EnvDiscreteRgbSeed plus 1.
public static vec3d_t GetDiscreteRGB_Env(double saturation = 1, double light = 0.5)
Parameters
Returns
- vec3d_t
RGB
GetDiscreteRgb(int, double, double)
Get discrete color(RGB). The term ‘discrete’ means there is big color difference between nearby seed. This function is good to auto set color for lot of components.
public static vec3d_t GetDiscreteRgb(int seed, double saturation = 1, double light = 0.5)
Parameters
seedintColor seed. Determines the hue by GetDiscreteHue(int).
saturationdoubleHSL saturation
lightdoubleHSL light
Returns
- vec3d_t
RGB
Remarks
The seed decides only the hue, by GetDiscreteHue(int);
saturation and light go to
HslToRgb(double, double, double) as given.
The same seed always gives the same color.
Seeds 0 to 377 give different 8-bit colors; beyond them two seeds can round to the same one.
GetDiscreteRgbByBoundary(int, double, double, double, double)
Get discrete RGB by boundary.
public static vec3d_t GetDiscreteRgbByBoundary(int seed, double hueBegin = 0, double hueEnd = 1, double saturation = 1, double light = 0.5)
Parameters
seedintColor seed. Determines the hue value.
hueBegindoubleThe beginning of the hue range.
hueEnddoubleThe end of the hue range.
saturationdoubleSaturation value of the color.
lightdoubleLight value of the color.
Returns
- vec3d_t
RGB color vector with boundary.
GetGloomyColor(Guid, double, double)
Gloomy color (blue tune by default) seeded by a persisted identity. The same Guid always yields the same color — across loads, threads, processes and platforms — unlike GetGloomyColor(object, double, double), whose identity-hash seed is stable only within one thread's allocation order.
public static vec3d_t GetGloomyColor(this Guid seed, double hueMin = 0.5, double hueMax = 0.7)
Parameters
seedGuidPersisted identity, e.g. an anchor's Guid.
hueMindoubleThe beginning of the hue range.
hueMaxdoubleThe end of the hue range.
Returns
- vec3d_t
RGB color vector.
Remarks
Uses an FNV-1a hash over ToByteArray() rather than GetHashCode() or GetHashCode(): the latter are not guaranteed stable across runtimes, and string hashing is randomized per process on .NET Core.
GetGloomyColor(object, double, double)
The default value is blue tune.
public static vec3d_t GetGloomyColor(this object seed, double hueMin = 0.5, double hueMax = 0.7)
Parameters
Returns
Remarks
The hue is seeded by GetHashCode(). For a type that does not override it, that is the CLR identity hash — drawn per thread on the first call — so the color depends on which thread asks first and in what order, and two loads of the same data get different colors. When a persisted identity is available, prefer GetGloomyColor(Guid, double, double).
GetRgb(double, RatioRgbFuncEnum)
Get RGB by funcEnum.
public static vec3d_t GetRgb(double v, RatioRgbFuncEnum funcEnum)
Parameters
vdoublevalue
funcEnumRatioRgbFuncEnumfunction enum
Returns
- vec3d_t
RGB
GetRgbByErf(double)
Get RGB interpolated from blue to green to red by erf function. The range suits for -2 to 0 to 2.
public static vec3d_t GetRgbByErf(double v)
Parameters
vdoubleinput of the erf
Returns
- vec3d_t
RGB
GetRgbByHslOffset(vec3d_t, vec3d_t)
Convert RGB by HSL offset.
This is a three step process: RGB convert to HSL, HSL+=hslOffset, HSL convert to RGB.
public static vec3d_t GetRgbByHslOffset(vec3d_t rgb, vec3d_t hslOffset)
Parameters
Returns
- vec3d_t
RGB
GetRgbByLinearRatio(double)
0 ~ 0.5 ~ 1 is linearly interpolated to blue to green to red. The below range data is pure blue. The exceeding range data is pure red.
public static vec3d_t GetRgbByLinearRatio(double ratio)
Parameters
ratiodoublethe interpolation range is 0~1
Returns
- vec3d_t
RGB
GetRgbByNormalizedErf(double)
0 ~ 0.5 ~ 1 is interpolated to blue to green to red.
The below range data is pure blue. The exceeding range data is pure red.
return GetRgbByLinearRatio(MathUtil.Erf(ratio*2) / MathUtil.Erf(2));
public static vec3d_t GetRgbByNormalizedErf(double ratio)
Parameters
ratiodoublethe interpolation range is 0~1
Returns
- vec3d_t
RGB
GetRgbByNormalizedPositiveErf(double)
0 ~ 0.5 ~ 1 is interpolated to blue to green to red.
The below range data is pure blue. The exceeding range data is pure red.
Only the positive half form is used.
return GetRgbByLinearRatio(MathUtil.Erf(ratio*2) / MathUtil.Erf(2));
public static vec3d_t GetRgbByNormalizedPositiveErf(double ratio)
Parameters
ratiodoublethe interpolation range is 0~1
Returns
- vec3d_t
RGB
GetRgbByPositiveErf(double)
Get RGB interpolated from blue to green to red by modified erf function.
public static vec3d_t GetRgbByPositiveErf(double v)
Parameters
vdoubleinput of the modified erf
Returns
- vec3d_t
color
Remarks
If v is equal or lower than 0, the color is blue;
if v is equal or larger than 1, the color is red;
otherwise, the color varied from blue to green to red by modified erf function.
GetRgbByPositiveErf(double, double, double)
Get RGB interpolated from blue to green to red by modified erf function.
public static vec3d_t GetRgbByPositiveErf(double v, double floor, double ceil)
Parameters
Returns
- vec3d_t
color
Remarks
If v is equal or lower than floor, the color is blue;
if v is equal or larger than ceil, the color is red;
otherwise, the color varied from blue to green to red by modified erf function.
HslToRgb(vec3d_t)
Convert color convention from HSL to RGB.
public static vec3d_t HslToRgb(vec3d_t hsl)
Parameters
hslvec3d_tHSL
Returns
- vec3d_t
RGB
HslToRgb(double, double, double)
Convert color convention from HSL to RGB.
public static vec3d_t HslToRgb(double hue, double saturation, double light)
Parameters
Returns
- vec3d_t
RGB
RgbToHsl(vec3d_t)
Convert color convention from RGB to HSL.
public static vec3d_t RgbToHsl(vec3d_t rgb)
Parameters
rgbvec3d_tRGB
Returns
- vec3d_t
HSL
RgbToHsl(double, double, double)
Convert color convention from RGB to HSL.
public static vec3d_t RgbToHsl(double r, double g, double b)
Parameters
Returns
- vec3d_t
HSL
ToDualHexString(vec3d_t)
Converts an RGB color vector to a hexadecimal string representation.
public static string ToDualHexString(vec3d_t rgb)
Parameters
rgbvec3d_tThe RGB color vector to convert.
Returns
- string
A hexadecimal string representation of the RGB color.