63 lines
2.0 KiB
C#
63 lines
2.0 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Drawing;
|
|
|
|
namespace ColorPickerApp
|
|
{
|
|
/// <summary>Genere une palette complete (teintes x nuances + niveaux de gris).</summary>
|
|
internal static class Palette
|
|
{
|
|
public static List<Color> Generate()
|
|
{
|
|
var colors = new List<Color>();
|
|
|
|
const int hueSteps = 12;
|
|
const int shadeSteps = 8;
|
|
|
|
for (int s = 0; s < shadeSteps; s++)
|
|
{
|
|
double lightness = 0.90 - (s * (0.80 / (shadeSteps - 1)));
|
|
for (int h = 0; h < hueSteps; h++)
|
|
{
|
|
double hue = (360.0 / hueSteps) * h;
|
|
colors.Add(FromHsl(hue, 0.75, lightness));
|
|
}
|
|
}
|
|
|
|
const int grayCount = 12;
|
|
for (int g = 0; g < grayCount; g++)
|
|
{
|
|
int v = (int)Math.Round(255.0 * g / (grayCount - 1));
|
|
colors.Add(Color.FromArgb(v, v, v));
|
|
}
|
|
|
|
return colors;
|
|
}
|
|
|
|
private static Color FromHsl(double h, double s, double l)
|
|
{
|
|
double c = (1 - Math.Abs(2 * l - 1)) * s;
|
|
double x = c * (1 - Math.Abs((h / 60.0) % 2 - 1));
|
|
double m = l - c / 2;
|
|
|
|
double r1, g1, b1;
|
|
if (h < 60) { r1 = c; g1 = x; b1 = 0; }
|
|
else if (h < 120) { r1 = x; g1 = c; b1 = 0; }
|
|
else if (h < 180) { r1 = 0; g1 = c; b1 = x; }
|
|
else if (h < 240) { r1 = 0; g1 = x; b1 = c; }
|
|
else if (h < 300) { r1 = x; g1 = 0; b1 = c; }
|
|
else { r1 = c; g1 = 0; b1 = x; }
|
|
|
|
int r = (int)Math.Round((r1 + m) * 255);
|
|
int g = (int)Math.Round((g1 + m) * 255);
|
|
int b = (int)Math.Round((b1 + m) * 255);
|
|
|
|
r = Math.Max(0, Math.Min(255, r));
|
|
g = Math.Max(0, Math.Min(255, g));
|
|
b = Math.Max(0, Math.Min(255, b));
|
|
|
|
return Color.FromArgb(r, g, b);
|
|
}
|
|
}
|
|
}
|