ReversiForm  1.0.0
ReversiForm
HslColor.cs
Go to the documentation of this file.
1 
18 using System;
19 using System.Drawing;
20 
21 namespace ReversiForm
22 {
28  public class HslColor
29  {
30  private float _h;
34  public float H
35  {
36  get { return this._h; }
37  }
38 
39  private float _s;
43  public float S
44  {
45  get { return this._s; }
46  }
47 
48  private float _l;
52  public float L
53  {
54  get { return this._l; }
55  }
56 
57  public HslColor(float hue, float saturation, float lightness)
58  {
59  if (hue < 0f || 360f <= hue)
60  {
61  throw new ArgumentException("hueは0以上360未満の値です。", "hue");
62  }
63  if (saturation < 0f || 1f < saturation)
64  {
65  throw new ArgumentException("saturationは0以上1以下の値です。", "saturation");
66  }
67  if (lightness < 0f || 1f < lightness)
68  {
69  throw new ArgumentException("lightnessは0以上1以下の値です。", "lightness");
70  }
71 
72  this._h = hue;
73  this._s = saturation;
74  this._l = lightness;
75  }
76 
82  public static HslColor FromRgb(Color rgb)
83  {
84  float r = (float)rgb.R / 255f;
85  float g = (float)rgb.G / 255f;
86  float b = (float)rgb.B / 255f;
87 
88  float max = Math.Max(r, Math.Max(g, b));
89  float min = Math.Min(r, Math.Min(g, b));
90 
91  float lightness = (max + min) / 2f;
92 
93  float hue, saturation;
94  if (max == min)
95  {
96  //undefined
97  hue = 0f;
98  saturation = 0f;
99  }
100  else
101  {
102  float c = max - min;
103 
104  if (max == r)
105  {
106  hue = (g - b) / c;
107  }
108  else if (max == g)
109  {
110  hue = (b - r) / c + 2f;
111  }
112  else
113  {
114  hue = (r - g) / c + 4f;
115  }
116  hue *= 60f;
117  if (hue < 0f)
118  {
119  hue += 360f;
120  }
121 
122  //saturation = c / (1f - Math.Abs(2f * lightness - 1f));
123  if (lightness < 0.5f)
124  {
125  saturation = c / (max + min);
126  }
127  else
128  {
129  saturation = c / (2f - max - min);
130  }
131  }
132 
133  return new HslColor(hue, saturation, lightness);
134  }
135 
141  public static Color ToRgb(HslColor hsl)
142  {
143  float s = hsl.S;
144  float l = hsl.L;
145 
146  float r1, g1, b1;
147  if (s == 0)
148  {
149  r1 = l;
150  g1 = l;
151  b1 = l;
152  }
153  else
154  {
155  float h = hsl.H / 60f;
156  int i = (int)Math.Floor(h);
157  float f = h - i;
158  //float c = (1f - Math.Abs(2f * l - 1f)) * s;
159  float c;
160  if (l < 0.5f)
161  {
162  c = 2f * s * l;
163  }
164  else
165  {
166  c = 2f * s * (1f - l);
167  }
168  float m = l - c / 2f;
169  float p = c + m;
170  //float x = c * (1f - Math.Abs(h % 2f - 1f));
171  float q; // q = x + m
172  if (i % 2 == 0)
173  {
174  q = l + c * (f - 0.5f);
175  }
176  else
177  {
178  q = l - c * (f - 0.5f);
179  }
180 
181  switch (i)
182  {
183  case 0:
184  r1 = p;
185  g1 = q;
186  b1 = m;
187  break;
188  case 1:
189  r1 = q;
190  g1 = p;
191  b1 = m;
192  break;
193  case 2:
194  r1 = m;
195  g1 = p;
196  b1 = q;
197  break;
198  case 3:
199  r1 = m;
200  g1 = q;
201  b1 = p;
202  break;
203  case 4:
204  r1 = q;
205  g1 = m;
206  b1 = p;
207  break;
208  case 5:
209  r1 = p;
210  g1 = m;
211  b1 = q;
212  break;
213  default:
214  throw new ArgumentException("色相の値が不正です。", "hsl");
215  }
216  }
217 
218  return Color.FromArgb(
219  (int)Math.Round(r1 * 255f),
220  (int)Math.Round(g1 * 255f),
221  (int)Math.Round(b1 * 255f));
222  }
223  }
224 }
HSL (HLS) カラーを表す
Definition: HslColor.cs:28
float S
彩度 (Saturation)
Definition: HslColor.cs:44
float L
輝度 (Lightness)
Definition: HslColor.cs:53
static Color ToRgb(HslColor hsl)
指定したHslColorからColorを作成する
Definition: HslColor.cs:141
float H
色相 (Hue)
Definition: HslColor.cs:35
static HslColor FromRgb(Color rgb)
指定したColorからHslColorを作成する
Definition: HslColor.cs:82