Skip to content
Snippets Groups Projects
Commit a2c48d7f authored by Erin O'Connell's avatar Erin O'Connell
Browse files

updated colorutils due to overflow issue

parent bc6fc54c
No related branches found
No related tags found
No related merge requests found
using System; using System;
using System.Drawing; using System.Drawing;
namespace OpenPixelControl namespace OpenPixelControl
{ {
public class HSLColor public class HSLColor
{ {
// Private data members below are on scale 0-1 // Private data members below are on scale 0-1
// They are scaled for use externally based on scale // They are scaled for use externally based on scale
private double hue = 1.0; private double hue = 1.0;
private double saturation = 1.0; private double saturation = 1.0;
private double luminosity = 1.0; private double luminosity = 1.0;
private const double scale = 240.0; private const double scale = 240.0;
public double Hue public double Hue
{ {
get { return hue * scale; } get { return hue * scale; }
set { hue = CheckRange(value / scale); } set { hue = CheckRange(value / scale); }
} }
public double Saturation public double Saturation
{ {
get { return saturation * scale; } get { return saturation * scale; }
set { saturation = CheckRange(value / scale); } set { saturation = CheckRange(value / scale); }
} }
public double Luminosity public double Luminosity
{ {
get { return luminosity * scale; } get { return luminosity * scale; }
set { luminosity = CheckRange(value / scale); } set { luminosity = CheckRange(value / scale); }
} }
private double CheckRange(double value) private double CheckRange(double value)
{ {
if (value < 0.0) if (value < 0.0)
value = 0.0; value = 0.0;
else if (value > 1.0) else if (value > 1.0)
value = 1.0; value = 1.0;
return value; return value;
} }
public override string ToString() public override string ToString()
{ {
return String.Format("H: {0:#0.##} S: {1:#0.##} L: {2:#0.##}", Hue, Saturation, Luminosity); return String.Format("H: {0:#0.##} S: {1:#0.##} L: {2:#0.##}", Hue, Saturation, Luminosity);
} }
public string ToRGBString() public string ToRGBString()
{ {
Color color = (Color)this; Color color = (Color)this;
return String.Format("R: {0:#0.##} G: {1:#0.##} B: {2:#0.##}", color.R, color.G, color.B); return String.Format("R: {0:#0.##} G: {1:#0.##} B: {2:#0.##}", color.R, color.G, color.B);
} }
public Pixel ToRgbPixel() public Pixel ToRgbPixel()
{ {
Color color = (Color)this; Color color = (Color)this;
return new Pixel((byte)(color.R - 1), (byte)(color.G - 1), (byte)(color.B - 1)); return new Pixel((byte)(color.R), (byte)(color.G), (byte)(color.B));
} }
#region Casts to/from System.Drawing.Color #region Casts to/from System.Drawing.Color
public static implicit operator Color(HSLColor hslColor) public static implicit operator Color(HSLColor hslColor)
{ {
double r = 0, g = 0, b = 0; double r = 0, g = 0, b = 0;
if (hslColor.luminosity != 0) if (hslColor.luminosity != 0)
{ {
if (hslColor.saturation == 0) if (hslColor.saturation == 0)
r = g = b = hslColor.luminosity; r = g = b = hslColor.luminosity;
else else
{ {
double temp2 = GetTemp2(hslColor); double temp2 = GetTemp2(hslColor);
double temp1 = 2.0 * hslColor.luminosity - temp2; double temp1 = 2.0 * hslColor.luminosity - temp2;
r = GetColorComponent(temp1, temp2, hslColor.hue + 1.0 / 3.0); r = GetColorComponent(temp1, temp2, hslColor.hue + 1.0 / 3.0);
g = GetColorComponent(temp1, temp2, hslColor.hue); g = GetColorComponent(temp1, temp2, hslColor.hue);
b = GetColorComponent(temp1, temp2, hslColor.hue - 1.0 / 3.0); b = GetColorComponent(temp1, temp2, hslColor.hue - 1.0 / 3.0);
} }
} }
return Color.FromArgb((int)(255 * r), (int)(255 * g), (int)(255 * b)); return Color.FromArgb((int)(255 * r), (int)(255 * g), (int)(255 * b));
} }
private static double GetColorComponent(double temp1, double temp2, double temp3) private static double GetColorComponent(double temp1, double temp2, double temp3)
{ {
temp3 = MoveIntoRange(temp3); temp3 = MoveIntoRange(temp3);
if (temp3 < 1.0 / 6.0) if (temp3 < 1.0 / 6.0)
return temp1 + (temp2 - temp1) * 6.0 * temp3; return temp1 + (temp2 - temp1) * 6.0 * temp3;
else if (temp3 < 0.5) else if (temp3 < 0.5)
return temp2; return temp2;
else if (temp3 < 2.0 / 3.0) else if (temp3 < 2.0 / 3.0)
return temp1 + ((temp2 - temp1) * ((2.0 / 3.0) - temp3) * 6.0); return temp1 + ((temp2 - temp1) * ((2.0 / 3.0) - temp3) * 6.0);
else else
return temp1; return temp1;
} }
private static double MoveIntoRange(double temp3) private static double MoveIntoRange(double temp3)
{ {
if (temp3 < 0.0) if (temp3 < 0.0)
temp3 += 1.0; temp3 += 1.0;
else if (temp3 > 1.0) else if (temp3 > 1.0)
temp3 -= 1.0; temp3 -= 1.0;
return temp3; return temp3;
} }
private static double GetTemp2(HSLColor hslColor) private static double GetTemp2(HSLColor hslColor)
{ {
double temp2; double temp2;
if (hslColor.luminosity < 0.5) //<=?? if (hslColor.luminosity < 0.5) //<=??
temp2 = hslColor.luminosity * (1.0 + hslColor.saturation); temp2 = hslColor.luminosity * (1.0 + hslColor.saturation);
else else
temp2 = hslColor.luminosity + hslColor.saturation - (hslColor.luminosity * hslColor.saturation); temp2 = hslColor.luminosity + hslColor.saturation - (hslColor.luminosity * hslColor.saturation);
return temp2; return temp2;
} }
public static implicit operator HSLColor(Color color) public static implicit operator HSLColor(Color color)
{ {
HSLColor hslColor = new HSLColor(); HSLColor hslColor = new HSLColor();
hslColor.hue = color.GetHue() / 360.0; // we store hue as 0-1 as opposed to 0-360 hslColor.hue = color.GetHue() / 360.0; // we store hue as 0-1 as opposed to 0-360
hslColor.luminosity = color.GetBrightness(); hslColor.luminosity = color.GetBrightness();
hslColor.saturation = color.GetSaturation(); hslColor.saturation = color.GetSaturation();
return hslColor; return hslColor;
} }
#endregion #endregion
public void SetRGB(int red, int green, int blue) public void SetRGB(int red, int green, int blue)
{ {
HSLColor hslColor = (HSLColor)Color.FromArgb(red, green, blue); HSLColor hslColor = (HSLColor)Color.FromArgb(red, green, blue);
this.hue = hslColor.hue; this.hue = hslColor.hue;
this.saturation = hslColor.saturation; this.saturation = hslColor.saturation;
this.luminosity = hslColor.luminosity; this.luminosity = hslColor.luminosity;
} }
public HSLColor() { } public HSLColor() { }
public HSLColor(Color color) public HSLColor(Color color)
{ {
SetRGB(color.R, color.G, color.B); SetRGB(color.R, color.G, color.B);
} }
public HSLColor(int red, int green, int blue) public HSLColor(int red, int green, int blue)
{ {
SetRGB(red, green, blue); SetRGB(red, green, blue);
} }
public HSLColor(double hue, double saturation, double luminosity) public HSLColor(double hue, double saturation, double luminosity)
{ {
this.Hue = hue; this.Hue = hue;
this.Saturation = saturation; this.Saturation = saturation;
this.Luminosity = luminosity; this.Luminosity = luminosity;
} }
} }
} }
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment