Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/Colors/ColorInput.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ public sealed class ColorInput
private readonly string _value0;
private readonly int _value1;
private readonly TinyColor _value2;
private readonly FastColor _value3;
private string _type;

public ColorInput()
{
Expand All @@ -27,6 +29,7 @@ private ColorInput(
string value0 = default,
int value1 = default,
TinyColor value2 = default,
FastColor value3 = default,
RGB rgb = default,
RGBA rgba = default,
HSL hsl = default,
Expand All @@ -38,51 +41,59 @@ private ColorInput(
_value0 = value0;
_value1 = value1;
_value2 = value2;
_value3 = value3;

if (rgb != default)
{
R = rgb.R;
G = rgb.G;
B = rgb.B;
_type = "rgb";
}
if (rgba != default)
{
R = rgba.R;
G = rgba.G;
B = rgba.B;
A = rgba.A;
_type = "rgb";
}
if (hsl != default)
{
H = hsl.H;
S = hsl.S;
L = hsl.L;
_type = "hsl";
}
if (hsla != default)
{
H = hsla.H;
S = hsla.S;
L = hsla.L;
A = hsla.A;
_type = "hsl";
}
if (hsv != default)
{
H = hsv.H;
S = hsv.S;
V = hsv.V;
_type = "hsv";
}
if (hsva != default)
{
H = hsva.H;
S = hsva.S;
V = hsva.V;
A = hsva.A;
_type = "hsv";
}
}

public static implicit operator ColorInput(string value) => new(0, value0: value);
public static implicit operator ColorInput(int value) => new(1, value1: value);
public static implicit operator ColorInput(TinyColor value) => new(2, value2: value);
public static implicit operator ColorInput(FastColor value) => new(3, value3: value);
public static implicit operator ColorInput(RGB value) => new(-1, rgb: value);
public static implicit operator ColorInput(RGBA value) => new(-1, rgba: value);
public static implicit operator ColorInput(HSL value) => new(-1, hsl: value);
Expand All @@ -109,6 +120,8 @@ private ColorInput(

public bool IsColor => _index == 2;

public bool IsFastColor => _index == 3;

public string AsString =>
_index == 0 ?
_value0 :
Expand All @@ -124,6 +137,16 @@ private ColorInput(
_value2 :
throw new InvalidOperationException();

public FastColor AsFastColor =>
_index == 3 ?
_value3 :
throw new InvalidOperationException();

public bool Is(string type)
{
return type == _type;
}

public override string ToString()
{
return _index switch
Expand Down
Loading
Loading