첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
유용한 소스 코드가 있으면 icodebroker@naver.com으로 보내주시면 감사합니다.
블로그 자료는 자유롭게 사용하세요.

■ 투명도와 채도를 적용한 색상 휠 사용하기

------------------------------------------------------------------------------------------------------------------------


TestProject.zip


MainForm.cs

 

 

using System;

using System.Drawing;

using System.Drawing.Drawing2D;

using System.Windows.Forms;

 

namespace TestProject

{

    /// <summary>

    /// 메인 폼

    /// </summary>

    public partial class MainForm : Form

    {

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor

        ////////////////////////////////////////////////////////////////////////////////////////// Public

 

        #region 생성자 - MainForm()

 

        /// <summary>

        /// 생성자

        /// </summary>

        public MainForm()

        {

            InitializeComponent();

 

            #region 이벤트를 설정한다.

 

            Load                             += Form_Load;

            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 투명도 수평 스크롤바 스크롤시 처리하기 - 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;

 

            Bitmap bitmap = new Bitmap(width, height);

 

            using(Graphics graphics = Graphics.FromImage(bitmap))

            {

                graphics.SmoothingMode = SmoothingMode.AntiAlias;

 

                DrawGrid(graphics, width, height);

 

                DrawColorWheel(graphics, Color.White, 0, 0, width, height);

            }

 

            this.canvasPictureBox.Image = bitmap;

        }

 

        #endregion

    }

}

 

------------------------------------------------------------------------------------------------------------------------

Posted by 사용자 icodebroker

댓글을 달아 주세요