728x90
반응형
728x170
▶ ColorWheelForm.cs
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
namespace TestProject
{
/// <summary>
/// 색상 휠 폼
/// </summary>
public partial class ColorWheelForm : Form
{
//////////////////////////////////////////////////////////////////////////////////////////////////// Field
////////////////////////////////////////////////////////////////////////////////////////// Private
#region Field
/// <summary>
/// 소스 비트맵
/// </summary>
private Bitmap sourceBitmap = null;
/// <summary>
/// 선택 색상
/// </summary>
private Color selectedColor = SystemColors.Control;
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Property
////////////////////////////////////////////////////////////////////////////////////////// Public
#region 선택 색상 - SelectedColor
/// <summary>
/// 선택 색상
/// </summary>
public Color SelectedColor
{
get
{
return this.selectedColor;
}
set
{
this.selectedColor = value;
this.selectColorPictureBox.Image = GetSampleBitmap
(
value,
this.selectColorPictureBox.ClientSize.Width,
this.selectColorPictureBox.ClientSize.Height
);
this.alphaHScrollBar.Value = value.A;
this.alphaTextBox.Text = this.alphaHScrollBar.Value.ToString();
}
}
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
////////////////////////////////////////////////////////////////////////////////////////// Public
#region 생성자 - MainForm()
/// <summary>
/// 생성자
/// </summary>
public ColorWheelForm()
{
InitializeComponent();
#region 이벤트를 설정한다.
Load += Form_Load;
this.canvasPictureBox.MouseMove += canvasPictureBox_MouseMove;
this.canvasPictureBox.MouseClick += canvasPictureBox_MouseClick;
this.alphaHScrollBar.Scroll += alphaHScrollBar_Scroll;
this.saturationHScrollBar.Scroll += saturationHScrollBar_Scroll;
#endregion
}
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Method
////////////////////////////////////////////////////////////////////////////////////////// Private
//////////////////////////////////////////////////////////////////////////////// Event
#region 폼 로드시 처리하기 - Form_Load(sender, e)
/// <summary>
/// 폼 로드시 처리하기
/// </summary>
/// <param name="sender">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
private void Form_Load(object sender, EventArgs e)
{
DrawColorWheel();
}
#endregion
#region 캔버스 픽처 박스 마우스 이동시 처리하기 - canvasPictureBox_MouseMove(sender, e)
/// <summary>
/// 캔버스 픽처 박스 마우스 이동시 처리하기
/// </summary>
/// <param name="sender">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
private void canvasPictureBox_MouseMove(object sender, MouseEventArgs e)
{
this.sampleColorPictureBox.Image = GetSampleBitmap
(
GetColor(e.X, e.Y),
this.sampleColorPictureBox.ClientSize.Width,
this.sampleColorPictureBox.ClientSize.Height
);
}
#endregion
#region 캔버스 픽처 박스 마우스 클릭시 처리하기 - canvasPictureBox_MouseClick(sender, e)
/// <summary>
/// 캔버스 픽처 박스 마우스 클릭시 처리하기
/// </summary>
/// <param name="sender">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
private void canvasPictureBox_MouseClick(object sender, MouseEventArgs e)
{
this.selectedColor = GetColor(e.X, e.Y);
}
#endregion
#region 투명도 수평 스크롤바 스크롤시 처리하기 - alphaHScrollBar_Scroll(sender, e)
/// <summary>
/// 투명도 수평 스크롤바 스크롤시 처리하기
/// </summary>
/// <param name="sender">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
private void alphaHScrollBar_Scroll(object sender, ScrollEventArgs e)
{
this.alphaTextBox.Text = this.alphaHScrollBar.Value.ToString();
DrawColorWheel();
}
#endregion
#region 채도 수평 스크롤바 스크롤시 처리하기 - saturationHScrollBar_Scroll(sender, e)
/// <summary>
/// 채도 수평 스크롤바 스크롤시 처리하기
/// </summary>
/// <param name="sender">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
private void saturationHScrollBar_Scroll(object sender, ScrollEventArgs e)
{
this.saturationTextBox.Text = this.saturationHScrollBar.Value.ToString();
DrawColorWheel();
}
#endregion
//////////////////////////////////////////////////////////////////////////////// Function
#region 그리드 그리기 - DrawGrid(graphics, width, height)
/// <summary>
/// 그리드 그리기
/// </summary>
/// <param name="graphics">그래픽스</param>
/// <param name="width">너비</param>
/// <param name="height">높이</param>
private void DrawGrid(Graphics graphics, int width, int height)
{
graphics.Clear(Color.White);
using(Pen pen = new Pen(Color.Black, 2))
{
for(int x = 10; x < width; x += 20)
{
graphics.DrawLine(pen, x, 0, x, height - 1);
}
for(int y = 10; y < height; y += 20)
{
graphics.DrawLine(pen, 0, y, width - 1, y);
}
}
}
#endregion
#region 서라운드 색상 배열 설정하기 - SetSurroundColorArray(surroundColorArray, index, stopPoint,
fromAlpha, fromRed, fromGreen, fromBlue, toAlpha, toRed, toGreen, toBlue)
/// <summary>
/// 서라운드 색상 배열 설정하기
/// </summary>
/// <param name="surroundColorArray">서라운드 색상 배열</param>
/// <param name="index">인덱스</param>
/// <param name="stopPoint">중단 포인트</param>
/// <param name="fromAlpha">FROM 투명도</param>
/// <param name="fromRed">FROM 빨간색</param>
/// <param name="fromGreen">FROM 녹색</param>
/// <param name="fromBlue">FROM 파란색</param>
/// <param name="toAlpha">TO 투명도</param>
/// <param name="toRed">TO 빨간색</param>
/// <param name="toGreen">TO 녹색</param>
/// <param name="toBlue">TO 파란색</param>
private void SetSurroundColorArray(Color[] surroundColorArray, ref int index, float stopPoint,
int fromAlpha, int fromRed, int fromGreen, int fromBlue, int toAlpha, int toRed, int toGreen, int toBlue)
{
int pointCount = (int)stopPoint - index;
float alpha = fromAlpha;
float red = fromRed;
float green = fromGreen;
float blue = fromBlue;
float deltaAlpha = (toAlpha - fromAlpha) / (pointCount - 1);
float deltaRed = (toRed - fromRed ) / (pointCount - 1);
float deltaGreen = (toGreen - fromGreen) / (pointCount - 1);
float deltaBlue = (toBlue - fromBlue ) / (pointCount - 1);
for(int i = 0; i < pointCount; i++)
{
surroundColorArray[index++] = Color.FromArgb((int)alpha, (int)red, (int)green, (int)blue);
alpha += deltaAlpha;
red += deltaRed;
green += deltaGreen;
blue += deltaBlue;
}
}
#endregion
#region 색상 휠 그리기 - DrawColorWheel(graphics, outlineColor, minimumX, minimumY, width, height)
/// <summary>
/// 색상 휠 그리기
/// </summary>
/// <param name="graphics">그래픽스</param>
/// <param name="outlineColor">외곽선 색상</param>
/// <param name="minimumX">최소 X 좌표</param>
/// <param name="minimumY">최소 Y 좌표</param>
/// <param name="width">너비</param>
/// <param name="height">높이</param>
private void DrawColorWheel(Graphics graphics, Color outlineColor, int minimumX, int minimumY, int width, int height)
{
Rectangle rectangle = new Rectangle(minimumX, minimumY, width, height);
GraphicsPath wheelGraphicsPath = new GraphicsPath();
wheelGraphicsPath.AddEllipse(rectangle);
wheelGraphicsPath.Flatten();
int alpha = this.alphaHScrollBar.Value;
int saturation = this.saturationHScrollBar.Value;
float pointCount = (wheelGraphicsPath.PointCount - 1) / 6;
Color[] surroundColorArray = new Color[wheelGraphicsPath.PointCount];
int index = 0;
SetSurroundColorArray
(
surroundColorArray,
ref index,
1 * pointCount,
alpha,
saturation,
0,
0,
alpha,
saturation,
0,
saturation
);
SetSurroundColorArray
(
surroundColorArray,
ref index,
2 * pointCount,
alpha,
saturation,
0,
saturation,
alpha,
0,
0,
saturation
);
SetSurroundColorArray
(
surroundColorArray,
ref index,
3 * pointCount,
alpha,
0,
0,
saturation,
alpha,
0,
saturation,
saturation
);
SetSurroundColorArray
(
surroundColorArray,
ref index,
4 * pointCount,
alpha,
0,
saturation,
saturation,
alpha,
0,
saturation,
0
);
SetSurroundColorArray
(
surroundColorArray,
ref index,
5 * pointCount,
alpha,
0,
saturation,
0,
alpha,
saturation,
saturation,
0
);
SetSurroundColorArray
(
surroundColorArray,
ref index,
wheelGraphicsPath.PointCount,
alpha,
saturation,
saturation,
0,
alpha,
saturation,
0,
0
);
using (PathGradientBrush pathBrush = new PathGradientBrush(wheelGraphicsPath))
{
pathBrush.CenterColor = Color.FromArgb(alpha, 255, 255, 255);
pathBrush.SurroundColors = surroundColorArray;
graphics.FillPath(pathBrush, wheelGraphicsPath);
using(Pen thickPen = new Pen(outlineColor, 2))
{
graphics.DrawPath(thickPen, wheelGraphicsPath);
}
}
}
#endregion
#region 색상 휠 그리기 - DrawColorWheel()
/// <summary>
/// 색상 휠 그리기
/// </summary>
private void DrawColorWheel()
{
int width = this.canvasPictureBox.Width;
int height = this.canvasPictureBox.Height;
this.sourceBitmap = new Bitmap(width, height);
using(Graphics graphics = Graphics.FromImage(this.sourceBitmap))
{
graphics.SmoothingMode = SmoothingMode.AntiAlias;
DrawColorWheel(graphics, Color.White, 0, 0, width, height);
}
Bitmap displayBitmap = new Bitmap(width, height);
using(Graphics graphics = Graphics.FromImage(displayBitmap))
{
graphics.SmoothingMode = SmoothingMode.AntiAlias;
DrawGrid(graphics, width, height);
DrawColorWheel(graphics, Color.White, 0, 0, width, height);
}
this.canvasPictureBox.Image = displayBitmap;
}
#endregion
#region 색상 구하기 - GetColor(x, y)
/// <summary>
/// 색상 구하기
/// </summary>
/// <param name="x">X 좌표</param>
/// <param name="y">Y 좌표</param>
/// <returns>색상</returns>
private Color GetColor(int x, int y)
{
int width = this.canvasPictureBox.Width;
float centetX = width / 2f;
float centerY = width / 2f;
float deltaX = centetX - x;
float deltaY = centerY - y;
if(deltaX * deltaX + deltaY * deltaY > centetX * centetX)
{
return BackColor;
}
return this.sourceBitmap.GetPixel(x, y);
}
#endregion
#region 샘플 비트맵 구하기 - GetSampleBitmap(color, width, height)
/// <summary>
/// 샘플 비트맵 구하기
/// </summary>
/// <param name="color">색상</param>
/// <param name="width">너비</param>
/// <param name="height">높이</param>
/// <returns>샘플 비트맵</returns>
private Bitmap GetSampleBitmap(Color color, int width, int height)
{
Bitmap bitmap = new Bitmap(width, height);
using(Graphics graphics = Graphics.FromImage(bitmap))
{
DrawGrid(graphics, width, height);
using(Brush brush = new SolidBrush(color))
{
graphics.FillRectangle(brush, 0, 0, width, height);
}
}
return bitmap;
}
#endregion
}
}
728x90
▶ MainForm.cs
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
namespace TestProject
{
/// <summary>
/// 메인 폼
/// </summary>
public partial class MainForm : Form
{
//////////////////////////////////////////////////////////////////////////////////////////////////// Field
////////////////////////////////////////////////////////////////////////////////////////// Private
#region Field
/// <summary>
/// 색상 1
/// </summary>
private Color color1 = Color.FromArgb(128, 255, 0, 0);
/// <summary>
/// 색상 2
/// </summary>
private Color color2 = Color.FromArgb(128, 0, 0, 255);
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
////////////////////////////////////////////////////////////////////////////////////////// Public
#region 생성자 - MainForm()
/// <summary>
/// 생성자
/// </summary>
public MainForm()
{
InitializeComponent();
#region 이벤트를 설정한다.
this.color1Button.Click += color1Button_Click;
this.color2Button.Click += color2Button_Click;
this.samplePictrueBox.Resize += samplePictureBox_Resize;
this.samplePictrueBox.Paint += samplePictureBox_Paint;
#endregion
}
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Method
////////////////////////////////////////////////////////////////////////////////////////// Private
//////////////////////////////////////////////////////////////////////////////// Event
#region 색상 1 버튼 클릭시 처리하기 - color1Button_Click(sender, e)
/// <summary>
/// 색상 1 버튼 클릭시 처리하기
/// </summary>
/// <param name="sender">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
private void color1Button_Click(object sender, EventArgs e)
{
ColorWheelForm form = new ColorWheelForm();
form.SelectedColor = this.color1;
if(form.ShowDialog() == DialogResult.OK)
{
this.color1 = form.SelectedColor;
}
samplePictrueBox.Refresh();
}
#endregion
#region 색상 2 버튼 클릭시 처리하기 - color2Button_Click(sender, e)
/// <summary>
/// 색상 2 버튼 클릭시 처리하기
/// </summary>
/// <param name="sender">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
private void color2Button_Click(object sender, EventArgs e)
{
ColorWheelForm form = new ColorWheelForm();
form.SelectedColor = this.color2;
if(form.ShowDialog() == DialogResult.OK)
{
this.color2 = form.SelectedColor;
}
this.samplePictrueBox.Refresh();
}
#endregion
#region 샘플 픽처 박스 크기 조정시 처리하기 - samplePictureBox_Resize(sender, e)
/// <summary>
/// 샘플 픽처 박스 크기 조정시 처리하기
/// </summary>
/// <param name="sender">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
private void samplePictureBox_Resize(object sender, EventArgs e)
{
this.samplePictrueBox.Refresh();
}
#endregion
#region 샘플 픽처 박스 페인트시 처리하기 - samplePictureBox_Paint(sender, e)
/// <summary>
/// 샘플 픽처 박스 페인트시 처리하기
/// </summary>
/// <param name="sender">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
private void samplePictureBox_Paint(object sender, PaintEventArgs e)
{
e.Graphics.Clear(Color.White);
int width = this.samplePictrueBox.ClientSize.Width;
int height = this.samplePictrueBox.ClientSize.Height;
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
using(Pen pen = new Pen(Color.Black, 3))
{
for(int x = 10; x <= width; x += 20)
{
e.Graphics.DrawLine(pen, x, 0, x, height);
}
for(int y = 10; y <= height; y += 20)
{
e.Graphics.DrawLine(pen, 0, y, width, y);
}
}
int third = this.samplePictrueBox.ClientSize.Width / 3;
using(Brush brush = new SolidBrush(this.color1))
{
e.Graphics.FillEllipse(brush, 0, 0, 2 * third, height);
}
using(Brush brush = new SolidBrush(this.color2))
{
e.Graphics.FillEllipse(brush, third, 0, 2 * third, height);
}
}
#endregion
}
}
728x90
반응형
그리드형(광고전용)
'C# > WinForm' 카테고리의 다른 글
[C#/WINFORM] 배경 이미지 움직이기 (0) | 2018.12.31 |
---|---|
[C#/WINFORM] StringFormat 클래스 : FormatFlags 속성을 사용해 문자열 라인 클리핑 설정하기 (0) | 2018.12.31 |
[C#/WINFORM] StringFormat 클래스 : Trimming 속성을 사용해 문자열 잘라내는 방법(String Trimming) 설정하기 (0) | 2018.12.31 |
[C#/WINFORM] 마우스 아래 파이 슬라이스 구하기 (0) | 2018.12.30 |
[C#/WINFORM] 프린터 해상도 설정하기 (0) | 2018.12.30 |
[C#/WINFORM] 투명도와 채도를 적용한 색상 휠 사용하기 (0) | 2018.12.30 |
[C#/WINFORM] 색상 휠 그리기 (0) | 2018.12.30 |
[C#/WINFORM] 색상 휠 그리기 (0) | 2018.12.30 |
[C#/WINFORM] 시스템 아이콘 사용하기 (0) | 2018.12.30 |
[C#/WINFORM] 정규 분포 곡선(Normal Distribution Curve) 그리기 (0) | 2018.12.30 |
댓글을 달아 주세요