Skip to content

Instantly share code, notes, and snippets.

@bibohlog
Created November 12, 2017 06:19
Show Gist options
  • Save bibohlog/025f85a1db464f33405e19ea068b6f68 to your computer and use it in GitHub Desktop.
Save bibohlog/025f85a1db464f33405e19ea068b6f68 to your computer and use it in GitHub Desktop.
HSBからRGBへ変換する (コピペ用)
/// <summary>
/// Get System.Windows.Media.Color from Hue / Saturation / Brightness of double values without "if" statements
/// </summary>
/// <param name="Hue">Hue in 0 to 360</param>
/// <param name="Saturation">Saturation in 0 to 100</param>
/// <param name="Brightness">Brightness(Value) in 0 to 100</param>
/// <returns>Color</returns>
Color GetColorFromHSB_(double Hue, double Saturation, double Brightness)
{
List<double> colors = new List<double> { 0, 0, 0 };
int CC = colors.Count;
#region << Hue(色相) >>
double RelativeInDegrees = Hue / (360 / CC);
int WholeNoPart = (int)Math.Truncate(RelativeInDegrees);
double FractionalPart = RelativeInDegrees % 1.0;
int StartIndex = WholeNoPart % CC;
int SecondIndex = (WholeNoPart + 1) % CC;
double val = FractionalPart * 2;
double color1 = Math.Min(2 - val, 1);
double color2 = Math.Min(val, 1);
colors[StartIndex] = 255 * color1;
colors[SecondIndex] = 255 * color2;
#endregion
#region << Saturation(彩度) >>
double satAs0to1 = Saturation / 100.0;
colors = colors.Select(item => item += (255 - item) * (1.0 - satAs0to1)).ToList();
#endregion
#region << Brightness / Value (輝度) >>
double brightAs0to1 = Brightness / 100.0;
colors = colors.Select(item => item *= brightAs0to1).ToList();
#endregion
var col = Color.FromArgb(255, (byte)colors[0], (byte)colors[1], (byte)colors[2]);
return col;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment