■ HSV 색상 선택기를 만드는 방법을 보여준다.
▶ Point2D.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
namespace TestProject; /// <summary> /// 2D 포인트 /// </summary> public class Point2D { //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region X 좌표 - X /// <summary> /// X 좌표 /// </summary> public double X { get; set; } #endregion #region Y 좌표 - Y /// <summary> /// Y 좌표 /// </summary> public double Y { get; set; } #endregion } |
▶ ColorPicker.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 |
using System.Drawing.Imaging; namespace TestProject; /// <summary> /// 색상 선택기 /// </summary> public class ColorPicker { //////////////////////////////////////////////////////////////////////////////////////////////////// Enumeration ////////////////////////////////////////////////////////////////////////////////////////// Public #region 영역 - Area /// <summary> /// 영역 /// </summary> public enum Area { /// <summary> /// 외부 /// </summary> Outside, /// <summary> /// 휠 /// </summary> Wheel, /// <summary> /// 삼각형 /// </summary> Triangle } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Structure ////////////////////////////////////////////////////////////////////////////////////////// Public #region 선택 결과 - PickResult /// <summary> /// 선택 결과 /// </summary> public struct PickResult { //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 영역 - Area /// <summary> /// 영역 /// </summary> public Area Area { get; set; } #endregion #region 색상 - Hue /// <summary> /// 색상 /// </summary> public double? Hue { get; set; } #endregion #region 채도 - Saturation /// <summary> /// 채도 /// </summary> public double? Saturation { get; set; } #endregion #region 값 - Value /// <summary> /// 값 /// </summary> public double? Value { get; set; } #endregion } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 크기 - Size /// <summary> /// 크기 /// </summary> public int Size { get; } #endregion #region 중앙 X - CenterX /// <summary> /// 중앙 X /// </summary> public int CenterX => Size / 2; #endregion #region 중앙 Y - CenterY /// <summary> /// 중앙 Y /// </summary> public int CenterY => Size / 2; #endregion #region 내부 반경 - InnerRadius /// <summary> /// 내부 반경 /// </summary> public int InnerRadius => Size * 5 / 12; #endregion #region 외부 반경 - OuterRadius /// <summary> /// 외부 반경 /// </summary> public int OuterRadius => Size / 2; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - ColorPicker(size) /// <summary> /// 생성자 /// </summary> /// <param name="size">크기</param> public ColorPicker(int size = 400) { Size = size; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Public #region 이미지 그리기 - DrawImage(hue, saturation, value) /// <summary> /// 이미지 그리기 /// </summary> /// <param name="hue">색상</param> /// <param name="saturation">채도</param> /// <param name="value">값</param> /// <returns>이미지</returns> public Image DrawImage(double hue = 0d, double saturation = 1d, double value = 1d) { Bitmap bitmap = new Bitmap(Size, Size, PixelFormat.Format32bppArgb); for(int y = 0; y < Size; y++) { for(int x = 0; x < Size; x++) { Color color; PickResult pickResult = Pick(x, y); if(pickResult.Area == Area.Outside) { color = Color.Transparent; } else if(pickResult.Area == Area.Wheel) { color = GetHSVColor(pickResult.Hue.Value, saturation, value, 1); } else { color = GetHSVColor(hue, pickResult.Saturation.Value, pickResult.Value.Value, 1); } bitmap.SetPixel(x, y, color); } } return bitmap; } #endregion #region 휠 위치 구하기 - GetWheelPosition(hue) /// <summary> /// 휠 위치 구하기 /// </summary> /// <param name="hue">색상</param> /// <returns>휠 위치</returns> public Point2D GetWheelPosition(double hue) { double middleRadius = (InnerRadius + OuterRadius) / 2; return new Point2D { X = CenterX + middleRadius * Math.Sin(hue), Y = CenterY - middleRadius * Math.Cos(hue) }; } #endregion #region 삼각형 위치 구하기 - GetTrianglePosition(saturation, value) /// <summary> /// 삼각형 위치 구하기 /// </summary> /// <param name="saturation">채도</param> /// <param name="value">값</param> /// <returns>삼각형 위치</returns> public Point2D GetTrianglePosition(double saturation, double value) { double squareRoot3 = Math.Sqrt(3); return new Point2D { X = CenterX + InnerRadius * (2 * value - saturation * value - 1) * squareRoot3 / 2, Y = CenterY + InnerRadius * (1 - 3 * saturation * value) / 2 }; } #endregion #region 선택하기 - Pick(x, y) /// <summary> /// 선택하기 /// </summary> /// <param name="x">X 좌표</param> /// <param name="y">Y 좌표</param> /// <returns>선택 결과</returns> public PickResult Pick(double x, double y) { double distanceFromCenter = Math.Sqrt((x - CenterX) * (x - CenterX) + (y - CenterY) * (y - CenterY)); double squareRoot3 = Math.Sqrt(3); if(distanceFromCenter > OuterRadius) { return new PickResult { Area = Area.Outside }; } else if(distanceFromCenter > InnerRadius) { double angle = Math.Atan2(y - CenterY, x - CenterX) + Math.PI / 2; if(angle < 0) { angle += 2 * Math.PI; } double hue = angle; return new PickResult { Area = Area.Wheel, Hue = hue }; } else { double x1 = (x - CenterX) * 1.0d / InnerRadius; double y1 = (y - CenterY) * 1.0d / InnerRadius; if(0 * x1 + 2 * y1 > 1) { return new PickResult { Area = Area.Outside }; } else if(squareRoot3 * x1 + (-1) * y1 > 1) { return new PickResult { Area = Area.Outside }; } else if(-squareRoot3 * x1 + (-1) * y1 > 1) { return new PickResult { Area = Area.Outside }; } else { double saturation = (1 - 2 * y1) / (squareRoot3 * x1 - y1 + 2); double value = (squareRoot3 * x1 - y1 + 2) / 3; return new PickResult { Area = Area.Triangle, Saturation = saturation, Value = value }; } } } #endregion ////////////////////////////////////////////////////////////////////////////////////////// Private #region RGB 색상 구하기 - GetRGBColor(red, green, blue, alpha) /// <summary> /// RGB 색상 구하기 /// </summary> /// <param name="red">적색</param> /// <param name="green">녹색</param> /// <param name="blue">청색</param> /// <param name="alpha">투명도</param> /// <returns>RGB 색상</returns> private Color GetRGBColor(double red, double green, double blue, double alpha) { return Color.FromArgb ( Math.Min(255, (int)(alpha * 256)), Math.Min(255, (int)(red * 256)), Math.Min(255, (int)(green * 256)), Math.Min(255, (int)(blue * 256)) ); } #endregion #region HSV 색상 구하기 - GetHSVColor(hue, saturation, value, alpha) /// <summary> /// HSV 색상 구하기 /// </summary> /// <param name="hue">색상</param> /// <param name="saturation">채도</param> /// <param name="value">값</param> /// <param name="alpha">알파</param> /// <returns>색상</returns> private Color GetHSVColor(double hue, double saturation, double value, double alpha) { double chroma = value * saturation; double step = Math.PI / 3; double interm = chroma * (1 - Math.Abs((hue / step) % 2.0 - 1)); double shift = value - chroma; if(hue < 1 * step) return GetRGBColor(shift + chroma, shift + interm, shift + 0 , alpha); if(hue < 2 * step) return GetRGBColor(shift + interm, shift + chroma, shift + 0 , alpha); if(hue < 3 * step) return GetRGBColor(shift + 0 , shift + chroma, shift + interm, alpha); if(hue < 4 * step) return GetRGBColor(shift + 0 , shift + interm, shift + chroma, alpha); if(hue < 5 * step) return GetRGBColor(shift + interm, shift + 0 , shift + chroma, alpha); return GetRGBColor(shift + chroma, shift + 0, shift + interm, alpha); } #endregion } |
▶ MainForm.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
namespace TestProject { /// <summary> /// 메인 폼 /// </summary> public partial class MainForm : Form { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainForm() /// <summary> /// 생성자 /// </summary> public MainForm() { InitializeComponent(); double hue = 3.3d; double saturation = 0.4d; double value = 0.9d; ColorPicker colorPicker = new ColorPicker(400); Image colorPickerImage = colorPicker.DrawImage(hue, saturation, value); using(Graphics imageGraphics = Graphics.FromImage(colorPickerImage)) { Pen pen = value < 0.5d ? Pens.White : Pens.Black; Point2D wheelPoint = colorPicker.GetWheelPosition(hue); imageGraphics.DrawEllipse(pen, (float)wheelPoint.X - 5, (float)wheelPoint.Y - 5, 10, 10); Point2D trianglePoint = colorPicker.GetTrianglePosition(saturation, value); imageGraphics.DrawEllipse(pen, (float)trianglePoint.X - 5, (float)trianglePoint.Y - 5, 10, 10); } this.pictureBox.Image = colorPickerImage; } #endregion } } |