728x90
반응형
728x170
▶ ColorMap.cs
using System;
namespace TestProject
{
/// <summary>
/// 색상 맵
/// </summary>
public class ColorMap
{
//////////////////////////////////////////////////////////////////////////////////////////////////// Field
////////////////////////////////////////////////////////////////////////////////////////// Private
#region Field
/// <summary>
/// 색상 카운트
/// </summary>
private int colorCount = 64;
/// <summary>
/// 불투명도
/// </summary>
private int alpha = 255;
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
////////////////////////////////////////////////////////////////////////////////////////// Public
#region 생성자 - ColorMap()
/// <summary>
/// 생성자
/// </summary>
public ColorMap()
{
}
#endregion
#region 생성자 - ColorMap(colorCount)
/// <summary>
/// 생성자
/// </summary>
/// <param name="colorCount">색상 카운트</param>
public ColorMap(int colorCount)
{
this.colorCount = colorCount;
}
#endregion
#region 생성자 - ColorMap(colorCount, alpha)
/// <summary>
/// 생성자
/// </summary>
/// <param name="colorCount">색상 카운트</param>
/// <param name="alpha">불투명도</param>
public ColorMap(int colorCount, int alpha)
{
this.colorCount = colorCount;
this.alpha = alpha;
}
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Method
////////////////////////////////////////////////////////////////////////////////////////// Public
#region SPRING 색상 값 배열 구하기 - GetSpringColorValueArray()
/// <summary>
/// SPRING 색상 값 배열 구하기
/// </summary>
/// <returns>SPRING 색상 값 배열</returns>
public int[,] GetSpringColorValueArray()
{
int[,] colorValueArray = new int[this.colorCount, 4];
float[] springArray = new float[this.colorCount];
for(int i = 0; i < this.colorCount; i++)
{
springArray[i] = 1.0f * i / (this.colorCount - 1);
colorValueArray[i, 0] = this.alpha;
colorValueArray[i, 1] = 255;
colorValueArray[i, 2] = (int)(255 * springArray[i]);
colorValueArray[i, 3] = 255 - colorValueArray[i, 1];
}
return colorValueArray;
}
#endregion
#region SUMMER 색상 값 배열 구하기 - GetSummerColorValueArray()
/// <summary>
/// SUMMER 색상 값 배열 구하기
/// </summary>
/// <returns>SUMMER 색상 값 배열</returns>
public int[,] GetSummerColorValueArray()
{
int[,] colorValueArray = new int[this.colorCount, 4];
float[] summerArray = new float[this.colorCount];
for(int i = 0; i < this.colorCount; i++)
{
summerArray[i] = 1.0f * i / (this.colorCount - 1);
colorValueArray[i, 0] = this.alpha;
colorValueArray[i, 1] = (int)(255 * summerArray[i]);
colorValueArray[i, 2] = (int)(255 * 0.5f * (1 + summerArray[i]));
colorValueArray[i, 3] = (int)(255 * 0.4f);
}
return colorValueArray;
}
#endregion
#region AUTUMN 색상 값 배열 구하기 - GetAutumnColorValueArray()
/// <summary>
/// AUTUMN 색상 값 배열 구하기
/// </summary>
/// <returns>AUTUMN 색상 값 배열</returns>
public int[,] GetAutumnColorValueArray()
{
int[,] colorValueArray = new int[this.colorCount, 4];
float[] autumnArray = new float[this.colorCount];
for(int i = 0; i < this.colorCount; i++)
{
autumnArray[i] = 1.0f * i / (this.colorCount - 1);
colorValueArray[i, 0] = this.alpha;
colorValueArray[i, 1] = 255;
colorValueArray[i, 2] = (int)(255 * autumnArray[i]);
colorValueArray[i, 3] = 0;
}
return colorValueArray;
}
#endregion
#region WINTER 색상 값 배열 구하기 - GetWinterColorValueArray()
/// <summary>
/// WINTER 색상 값 배열 구하기
/// </summary>
/// <returns>WINTER 색상 값 배열</returns>
public int[,] GetWinterColorValueArray()
{
int[,] colorValueArray = new int[this.colorCount, 4];
float[] winterArray = new float[this.colorCount];
for(int i = 0; i < this.colorCount; i++)
{
winterArray[i] = 1.0f * i / (this.colorCount - 1);
colorValueArray[i, 0] = this.alpha;
colorValueArray[i, 1] = 0;
colorValueArray[i, 2] = (int)(255 * winterArray[i]);
colorValueArray[i, 3] = (int)(255 * (1.0f - 0.5f * winterArray[i]));
}
return colorValueArray;
}
#endregion
#region GRAY 색상 값 배열 구하기 - GetGrayColorArray()
/// <summary>
/// GRAY 색상 값 배열 구하기
/// </summary>
/// <returns>GRAY 색상 값 배열</returns>
public int[,] GetGrayColorArray()
{
int[,] colorValueArray = new int[this.colorCount, 4];
float[] grayArray = new float[this.colorCount];
for(int i = 0; i < this.colorCount; i++)
{
grayArray[i] = 1.0f * i / (this.colorCount - 1);
colorValueArray[i, 0] = this.alpha;
colorValueArray[i, 1] = (int)(255 * grayArray[i]);
colorValueArray[i, 2] = (int)(255 * grayArray[i]);
colorValueArray[i, 3] = (int)(255 * grayArray[i]);
}
return colorValueArray;
}
#endregion
#region JET 색상 값 배열 구하기 - GetJetColorValueArray()
/// <summary>
/// JET 색상 값 배열 구하기
/// </summary>
/// <returns>JET 색상 값 배열</returns>
public int[,] GetJetColorValueArray()
{
int[,] colorValueArray = new int[this.colorCount, 4];
float[,] colorMatrix = new float[this.colorCount, 3];
int partCount = (int)Math.Ceiling(this.colorCount / 4.0f);
int partCountModular = 0;
float[] jetArray = new float[3 * partCount - 1];
int[] redArray = new int[jetArray.Length];
int[] greenArray = new int[jetArray.Length];
int[] blueArray = new int[jetArray.Length];
if(this.colorCount % 4 == 1)
{
partCountModular = 1;
}
for(int i = 0; i < jetArray.Length; i++)
{
if(i < partCount)
{
jetArray[i] = (float)(i + 1) / partCount;
}
else if(i >= partCount && i < 2 * partCount - 1)
{
jetArray[i] = 1.0f;
}
else if(i >= 2 * partCount - 1)
{
jetArray[i] = (float)(3 * partCount - 1 - i) / partCount;
}
greenArray[i] = (int)Math.Ceiling(partCount / 2.0f) - partCountModular + i;
redArray[i] = greenArray[i] + partCount;
blueArray[i] = greenArray[i] - partCount;
}
int blueCount = 0;
for(int i = 0; i < blueArray.Length; i++)
{
if(blueArray[i] > 0)
{
blueCount++;
}
}
for(int i = 0; i < this.colorCount; i++)
{
for(int j = 0; j < redArray.Length; j++)
{
if(i == redArray[j] && redArray[j] < this.colorCount)
{
colorMatrix[i, 0] = jetArray[i - redArray[0]];
}
}
for(int j = 0; j < greenArray.Length; j++)
{
if(i == greenArray[j] && greenArray[j] < this.colorCount)
{
colorMatrix[i, 1] = jetArray[i - (int)greenArray[0]];
}
}
for(int j = 0; j < blueArray.Length; j++)
{
if(i == blueArray[j] && blueArray[j] >= 0)
{
colorMatrix[i, 2] = jetArray[jetArray.Length - 1 - blueCount + i];
}
}
}
for(int i = 0; i < this.colorCount; i++)
{
colorValueArray[i, 0] = this.alpha;
for(int j = 0; j < 3; j++)
{
colorValueArray[i, j+1] = (int)(colorMatrix[i, j] * 255);
}
}
return colorValueArray;
}
#endregion
#region HOT 색상 값 배열 구하기 - GetHotColorValueArray()
/// <summary>
/// HOT 색상 값 배열 구하기
/// </summary>
/// <returns>HOT 색상 값 배열</returns>
public int[,] GetHotColorValueArray()
{
int[,] colorValueArray = new int[this.colorCount, 4];
int partCount = 3 * this.colorCount / 8;
float[] redArray = new float[this.colorCount];
float[] greenArray = new float[this.colorCount];
float[] blueArray = new float[this.colorCount];
for(int i = 0; i < this.colorCount; i++)
{
if(i < partCount)
{
redArray[i] = 1.0f * (i + 1) / partCount;
}
else
{
redArray[i] = 1.0f;
}
if(i < partCount)
{
greenArray[i] = 0f;
}
else if(i >= partCount && i < 2 * partCount)
{
greenArray[i] = 1.0f * (i+1 - partCount) / partCount;
}
else
{
greenArray[i] = 1f;
}
if(i < 2 * partCount)
{
blueArray[i] = 0f;
}
else
{
blueArray[i] = 1.0f * (i + 1 - 2 * partCount) / (this.colorCount - 2 * partCount);
}
colorValueArray[i, 0] = this.alpha;
colorValueArray[i, 1] = (int)(255 * redArray[i]);
colorValueArray[i, 2] = (int)(255 * greenArray[i]);
colorValueArray[i, 3] = (int)(255 * blueArray[i]);
}
return colorValueArray;
}
#endregion
#region COOL 색상 값 배열 구하기 - GetCoolColorValueArray()
/// <summary>
/// COOL 색상 값 배열 구하기
/// </summary>
/// <returns>COOL 색상 값 배열</returns>
public int[,] GetCoolColorValueArray()
{
int[,] colorValueArray = new int[this.colorCount, 4];
float[] coolArray = new float[this.colorCount];
for(int i = 0; i < this.colorCount; i++)
{
coolArray[i] = 1.0f * i / (this.colorCount - 1);
colorValueArray[i, 0] = this.alpha;
colorValueArray[i, 1] = (int)(255 * coolArray[i]);
colorValueArray[i, 2] = (int)(255 * (1 - coolArray[i]));
colorValueArray[i, 3] = 255;
}
return colorValueArray;
}
#endregion
}
}
728x90
▶ MainForm.cs
using System.Drawing;
using System.Windows.Forms;
namespace TestProject
{
/// <summary>
/// 메인 폼
/// </summary>
public partial class MainForm : Form
{
//////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
////////////////////////////////////////////////////////////////////////////////////////// Public
#region 생성자 - MainForm
/// <summary>
/// 생성자
/// </summary>
public MainForm()
{
InitializeComponent();
SetStyle(ControlStyles.ResizeRedraw, true);
}
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Method
////////////////////////////////////////////////////////////////////////////////////////// Protected
#region 페인트시 처리하기 - OnPaint(e)
/// <summary>
/// 페인트시 처리하기
/// </summary>
/// <param name="e">이벤트 발생자</param>
protected override void OnPaint(PaintEventArgs e)
{
Graphics graphics = e.Graphics;
int width = 30;
int height = 200;
int y = 10;
ColorMap colorMap1 = new ColorMap();
Font font = new Font("나눔고딕코딩", 20, FontStyle.Bold);
graphics.DrawString("OPAQUE COLOR", font, Brushes.Black, 10, 60);
DrawColorBar(graphics, 10 , y, width, height, colorMap1, "Spring");
DrawColorBar(graphics, 10 + 40 , y, width, height, colorMap1, "Summer");
DrawColorBar(graphics, 10 + 2 * 40, y, width, height, colorMap1, "Autumn");
DrawColorBar(graphics, 10 + 3 * 40, y, width, height, colorMap1, "Winter");
DrawColorBar(graphics, 10 + 4 * 40, y, width, height, colorMap1, "Jet" );
DrawColorBar(graphics, 10 + 5 * 40, y, width, height, colorMap1, "Gray" );
DrawColorBar(graphics, 10 + 6 * 40, y, width, height, colorMap1, "Hot" );
DrawColorBar(graphics, 10 + 7 * 40, y, width, height, colorMap1, "Cool" );
y = y + 220;
ColorMap colorMap2 = new ColorMap(64, 150);
graphics.DrawString("TRANSPARENT COLOR", font, Brushes.Black, 10, 270);
DrawColorBar(graphics, 10 , y, width, height, colorMap2, "Spring");
DrawColorBar(graphics, 10 + 40 , y, width, height, colorMap2, "Summer");
DrawColorBar(graphics, 10 + 2 * 40, y, width, height, colorMap2, "Autumn");
DrawColorBar(graphics, 10 + 3 * 40, y, width, height, colorMap2, "Winter");
DrawColorBar(graphics, 10 + 4 * 40, y, width, height, colorMap2, "Jet" );
DrawColorBar(graphics, 10 + 5 * 40, y, width, height, colorMap2, "Gray" );
DrawColorBar(graphics, 10 + 6 * 40, y, width, height, colorMap2, "Hot" );
DrawColorBar(graphics, 10 + 7 * 40, y, width, height, colorMap2, "Cool" );
}
#endregion
////////////////////////////////////////////////////////////////////////////////////////// Private
#region 색상바 그리기 - DrawColorBar(graphics, x, y, width, height, colorMap, text)
/// <summary>
/// 색상바 그리기
/// </summary>
/// <param name="graphics">그래픽스</param>
/// <param name="x">X 좌표</param>
/// <param name="y">Y 좌표</param>
/// <param name="width">너비</param>
/// <param name="height">높이</param>
/// <param name="colorMap">색상 맵</param>
/// <param name="text">텍스트</param>
private void DrawColorBar(Graphics graphics, int x, int y, int width, int height, ColorMap colorMap, string text)
{
int[,] colorValueArray = new int[64, 4];
switch(text)
{
case "Jet" : colorValueArray = colorMap.GetJetColorValueArray(); break;
case "Hot" : colorValueArray = colorMap.GetHotColorValueArray(); break;
case "Gray" : colorValueArray = colorMap.GetGrayColorArray(); break;
case "Cool" : colorValueArray = colorMap.GetCoolColorValueArray(); break;
case "Summer" : colorValueArray = colorMap.GetSummerColorValueArray(); break;
case "Autumn" : colorValueArray = colorMap.GetAutumnColorValueArray(); break;
case "Spring" : colorValueArray = colorMap.GetSpringColorValueArray(); break;
case "Winter" : colorValueArray = colorMap.GetWinterColorValueArray(); break;
}
int minimumY = 0;
int maximumY = 32;
int deltaY = height / (maximumY - minimumY);
int colorValueArrayLength = 64;
for(int i = 0; i < 32; i++)
{
int colorIndex = (int)((i - minimumY) * colorValueArrayLength / (maximumY - minimumY));
SolidBrush brush = new SolidBrush
(
Color.FromArgb
(
colorValueArray[colorIndex, 0],
colorValueArray[colorIndex, 1],
colorValueArray[colorIndex, 2],
colorValueArray[colorIndex, 3]
)
);
graphics.FillRectangle(brush, x, y + i * deltaY, width, deltaY);
}
}
#endregion
}
}
728x90
반응형
그리드형(광고전용)
'C# > WinForm' 카테고리의 다른 글
[C#/WINFORM] DataGridView 클래스 : 마우스를 사용해 항목 순서 변경하기 (0) | 2018.09.01 |
---|---|
[C#/WINFORM] 텐서플로우를 사용해 물체 인식하기 (0) | 2018.08.29 |
[C#/WINFORM] Control 클래스 : ProcessCmdKey 메소드를 사용해 COPY/PASTE/CUT 방지하기 (0) | 2018.08.15 |
[C#/WINFORM] TextBox 클래스 : ShortcustsEnabled 속성을 사용해 COPY/PASTE/CUT 방지하기 (0) | 2018.08.15 |
[C#/WINFORM] Control 클래스 : WndProc 메소드를 사용해 COPY/PASTE/CUT 방지하기 (0) | 2018.08.15 |
[C#/WINFORM] 별 그리기 (0) | 2018.04.14 |
[C#/WINFORM] Pen 클래스 : 1 픽셀 너비 펜 구하기 (0) | 2018.04.13 |
[C#/WINFORM] 무어의 이웃 등고선 추적 (Moore Neighbor Contour Tracing) 알고리즘 사용하기 (0) | 2018.04.12 |
[C#/WINFORM] DirectShow를 사용해 동영상 재생하기 (0) | 2018.04.02 |
[C#/WINFORM] 설치 프린터 조회하기 (0) | 2018.03.22 |
댓글을 달아 주세요