Skip to content

Commit

Permalink
add more test cases for TinyColor (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
yoli799480165 authored Dec 25, 2023
1 parent 1bd9366 commit 9537310
Show file tree
Hide file tree
Showing 5 changed files with 965 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/Colors/Format.cs
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ public static ColorInput StringInputToObject(string color)
R = ParseIntFromHex(match.Groups[1].Value),
G = ParseIntFromHex(match.Groups[2].Value),
B = ParseIntFromHex(match.Groups[3].Value),
A = ParseIntFromHex(match.Groups[4].Value),
A = ConvertHexToDecimal(match.Groups[4].Value),
Format = named ? "name" : "hex8",
};
}
Expand Down
17 changes: 14 additions & 3 deletions src/Colors/TinyColor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,17 @@ public static TinyColor FromRatio(RatioInput ratio, TinyColorOptions opts = null
return new TinyColor(newColor, opts);
}

public static TinyColor LegacyRandom()
{
var random = new Random();
return new TinyColor(new RGB
{
R = random.Next(),
G = random.Next(),
B = random.Next(),
});
}

public bool IsDark()
{
return GetBrightness() < 128;
Expand Down Expand Up @@ -145,7 +156,7 @@ public string ToHsvString()
public HSLA ToHsl()
{
var hsl = RgbToHsl(_r, _g, _b);
return new HSLA(hsl.H, hsl.S, hsl.L, _a);
return new HSLA(hsl.H * 360, hsl.S, hsl.L, _a);
}

public string ToHslString()
Expand Down Expand Up @@ -394,7 +405,7 @@ public TinyColor[] Analogous(double results = 6, double slices = 30)
var hsl = ToHsl();
var part = 360 / slices;
var ret = new List<TinyColor>() { this };
for (hsl.H = (hsl.H - ((int)(part * results) >> 1) + 720) % 360; results > 0; --results)
for (hsl.H = (hsl.H - ((int)(part * results) >> 1) + 720) % 360; results > 1; --results)
{
hsl.H = (hsl.H + part) % 360;
ret.Add(new TinyColor(hsl));
Expand Down Expand Up @@ -465,7 +476,7 @@ public TinyColor[] Triad()

public TinyColor[] Tetrad()
{
return Polyad(3);
return Polyad(4);
}

public TinyColor[] Polyad(double n)
Expand Down
52 changes: 47 additions & 5 deletions src/Colors/Util.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace CssInCSharp.Colors
{
internal static class Util
public static class Util
{
public static string CharAt(this string str, int index)
{
Expand All @@ -28,7 +28,7 @@ public static StringNumber Bound01(StringNumber n, double max)

if (isPercent)
{
n = n * max / 100;
n = (int)(n * max) / (double)100;
}

if (Math.Abs(n - max) < 0.000001)
Expand Down Expand Up @@ -78,7 +78,7 @@ public static double BoundAlpha(StringNumber a)

public static double ConvertHexToDecimal(string h)
{
return ParseIntFromHex(h) / 255;
return ParseIntFromHex(h) / (double)255;
}

public static int ParseIntFromHex(string h)
Expand Down Expand Up @@ -114,7 +114,30 @@ public static bool IsPercentage(StringNumber n)

public static double MathRound(double value)
{
return Math.Round(value, MidpointRounding.AwayFromZero);
if (value >= 0)
{
return Math.Round(value, MidpointRounding.AwayFromZero);
}
else
{
/*
* js math round method:
* Math.round(25.5) -> 26
* Math.round(-25.5) -> -25
*/
if (IsMidpoint(GetDecimalPlaces(value)))
{
#if NETSTANDARD2_1
return Math.Round(value, MidpointRounding.ToEven);
#else
return Math.Round(value, MidpointRounding.ToZero);
#endif
}
else
{
return Math.Round(value, MidpointRounding.AwayFromZero);
}
}
}

public static double MathMax(params double[] values)
Expand All @@ -131,5 +154,24 @@ public static double Clamp01(double val) {
return Math.Min(1, Math.Max(0, val));
}

}
public static bool IsMidpoint(double val)
{
var i = 0;
while (i < 16)
{
i++;
var mid = -(5 / Math.Pow(10, i));
if (val == mid)
{
return true;
}
}
return false;
}

public static double GetDecimalPlaces(double val)
{
return val - Math.Truncate(val);
}
}
}
Loading

0 comments on commit 9537310

Please sign in to comment.