using System;
namespace SBRL.Utilities
{
///
/// Represents a single random HSL(A) colour.
///
///
/// Includes a static generator method that generates random saturated colours.
///
/// v0.1, by Starbeamrainbowlabs
///
public class ColourHSL
{
///
/// A random number generator.Used to generate random colours in RandomSaturated().
///
private static Random random = new Random();
#region Properties
///
/// The hue of the colour, between 0 and 360.
///
public float Hue { get; set; }
///
/// The saturation of the colour, between 0 and 100.
///
public float Saturation { get; set; }
///
/// The lightness of the colour, between 0 and 100.
///
public float Lightness { get; set; }
///
/// The opacity of the colour, between 0 and 1.
///
public float Alpha { get; set; }
#endregion
#region Aliases
///
/// Alias of Hue.
///
public float H {
get { return Hue; }
set { Hue = value; }
}
///
/// Alias of Saturation.
///
public float S {
get { return Saturation; }
set { Saturation = value; }
}
///
/// Alias of Lightness.
///
public float L {
get { return Lightness; }
set { Lightness = value; }
}
///
/// Alias of Alpha.
///
public float A {
get { return Alpha; }
set { Alpha = value; }
}
#endregion
///
/// Whether this colour is opaque or not.
///
public bool IsOpaque {
get {
return Alpha == 1;
}
}
///
/// Whether this colour is translucent or not.
///
public bool IsTranslucent {
get {
return !IsOpaque && !IsTransparent;
}
}
///
/// Whether this colour is completely transparent or not.
///
public bool IsTransparent {
get {
return Alpha == 0;
}
}
///
/// Whether this colour is currently valid or not. A valid colour has a hue of 0-360,
/// a saturation of 0-100, a lightness of 0-100, and an opacity of 0-1.
///
public bool IsValidColour
{
get {
return Hue >= 0 && Hue <= 360 &&
Saturation >= 0 && Saturation <= 100 &&
Lightness >= 0 && Lightness <= 100 &&
Alpha >= 0 && Alpha <= 1;
}
}
///
/// Initializes a new ColourHSL instance, with the colour set to bright white.
///
public ColourHSL() : this(0, 100, 100, 1)
{
}
///
/// Initializes a new class instance.
///
/// The hue of the colour.
/// The saturation of the colour.
/// The lightness of the colour.
public ColourHSL(float inHue, float inSaturation, float inLightness) : this(inHue, inSaturation, inLightness, 1)
{
}
///
/// Initializes a new class instance.
///
/// The hue of the colour.
/// The saturation of the colour.
/// The lightness of the colour.
/// The opacity of the colour.
public ColourHSL(float inHue, float inSaturation, float inLightness, float inAlpha)
{
Hue = inHue;
Saturation = inSaturation;
Lightness = inLightness;
Alpha = inAlpha;
}
///
/// Returns a random bright colour.
///
public static ColourHSL RandomSaturated()
{
return new ColourHSL() {
Hue = random.Next(0, 360),
Lightness = random.Next(45, 55),
Saturation = random.Next(90, 100)
};
}
#region Overrides
///
/// Returns a that represents this instance.
///
/// A that represents this instance.
public override string ToString()
{
string format = string.Empty;
if (IsOpaque)
format = "hsl({0}, {1}%, {2}%)";
else
format = "hsla({0}, {1}%, {2}%, {3})";
return string.Format(format, Hue, Saturation, Lightness, Math.Round(Alpha, 3));
}
#endregion
}
}