Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added the method 'GetRandomColorExact' to be able to use the ColorHelper for example in LINQ and other places where it has to be more exact #12

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
30 changes: 29 additions & 1 deletion ColorHelper.Tests/Generator/ColorGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public void GetRandomColorHsl()
var result = ColorGenerator.GetRandomColor<HSL>();
Assert.NotNull((HSL)result);
}

[Test]
public void GetRandomColorXyz()
{
Expand All @@ -66,5 +66,33 @@ public void GetDarkRandomColor()
Assert.That(result.G, Is.LessThanOrEqualTo(80));
Assert.That(result.B, Is.LessThanOrEqualTo(80));
}

[Test]
[Repeat(100)]
public void GetRandomColorExact()
{
var result = ColorGenerator.GetRandomColorExact<RGB>("helloworld".GetHashCode());
Assert.NotNull((RGB)result);
}

[Test]
[Repeat(100)]
public void GetRandomColorExactLight()
{
var result = ColorGenerator.GetRandomColorExact<RGB>("helloworld".GetHashCode(), ColorGenerator.ColorThemesEnum.Light);
Assert.That(result.R, Is.GreaterThanOrEqualTo(170));
Assert.That(result.G, Is.GreaterThanOrEqualTo(170));
Assert.That(result.B, Is.GreaterThanOrEqualTo(170));
}

[Test]
[Repeat(100)]
public void GetRandomColorExactDark()
{
var result = ColorGenerator.GetRandomColorExact<RGB>("helloworld".GetHashCode(), ColorGenerator.ColorThemesEnum.Dark);
Assert.That(result.R, Is.LessThanOrEqualTo(80));
Assert.That(result.G, Is.LessThanOrEqualTo(80));
Assert.That(result.B, Is.LessThanOrEqualTo(80));
}
}
}
12 changes: 12 additions & 0 deletions ColorHelper/Color/ColorThemesEnum.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace ColorHelper
{
public static partial class ColorGenerator
{
public enum ColorThemesEnum
{
Default,
Light,
Dark
}
}
}
55 changes: 41 additions & 14 deletions ColorHelper/Generator/ColorGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,46 +2,73 @@

namespace ColorHelper
{
public static class ColorGenerator
public static partial class ColorGenerator
{
public static T GetRandomColor<T>() where T: IColor
{
return GetRandomColor<T>(new RgbRandomColorFilter());
return GetRandomColor<T>(GetColorFilter(ColorThemesEnum.Default));
}

public static T GetLightRandomColor<T>() where T : IColor
{
const byte minRangeValue = 170;
return GetRandomColor<T>(GetColorFilter(ColorThemesEnum.Light));
}

RgbRandomColorFilter filter = new RgbRandomColorFilter();
filter.minR = minRangeValue;
filter.minG = minRangeValue;
filter.minB = minRangeValue;
public static T GetDarkRandomColor<T>() where T : IColor
{
return GetRandomColor<T>(GetColorFilter(ColorThemesEnum.Dark));
}

return GetRandomColor<T>(filter);
public static T GetRandomColorExact<T>(int hashCode, ColorThemesEnum colorTheme = ColorThemesEnum.Default) where T : IColor
{
return GetRandomColorExact<T>(hashCode, GetColorFilter(colorTheme));
}

public static T GetDarkRandomColor<T>() where T : IColor
private static RgbRandomColorFilter GetColorFilter(ColorThemesEnum colorTheme)
{
const byte maxRangeValue = 80;
const byte minRangeValue = 170;

RgbRandomColorFilter filter = new RgbRandomColorFilter();
filter.maxR = maxRangeValue;
filter.maxG = maxRangeValue;
filter.maxB = maxRangeValue;

return GetRandomColor<T>(filter);
switch (colorTheme)
{
case ColorThemesEnum.Default:
break;
case ColorThemesEnum.Light:
filter.minR = minRangeValue;
filter.minG = minRangeValue;
filter.minB = minRangeValue;
break;
case ColorThemesEnum.Dark:
filter.maxR = maxRangeValue;
filter.maxG = maxRangeValue;
filter.maxB = maxRangeValue;
break;
}

return filter;
}

private static T GetRandomColor<T>(RgbRandomColorFilter filter) where T : IColor
{
Random random = new Random(DateTime.Now.Millisecond);
return GetRandomColor<T>(filter, random);
}

private static T GetRandomColorExact<T>(int hashCode, RgbRandomColorFilter filter) where T : IColor
{
Random random = new Random(DateTime.Now.Millisecond + hashCode);
return GetRandomColor<T>(filter, random);
}

private static T GetRandomColor<T>(RgbRandomColorFilter filter, Random random) where T : IColor
{
RGB rgb = new RGB(
(byte)random.Next(filter.minR, filter.maxR),
(byte)random.Next(filter.minG, filter.maxG),
(byte)random.Next(filter.minB, filter.maxB));

return ConvertRgbToNecessaryColorType<T>(rgb);
}

Expand Down