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

728x90
반응형

TestProject.zip
다운로드

▶ MainWindow.xaml

<Window x:Class="TestProject.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:TestProject"
    Width="800"
    Height="600"
    Title="LinearGradientBrush 클래스 : 오프셋 값을 사용해 색상 구하기"
    FontFamily="나눔고딕코딩"
    FontSize="16">
    <Grid>
        <StackPanel
            HorizontalAlignment="Center"
            VerticalAlignment="Center">
            <Rectangle Name="sourceRectangle"
                Width="400"
                Height="50">
                <Rectangle.Fill>
                    <LinearGradientBrush x:Name="sourceBrush"
                        StartPoint="0 0.5" EndPoint="1 0.5">
                        <GradientStop Offset="0"   Color="Blue"   />
                        <GradientStop Offset="0.5" Color="Green"  />
                        <GradientStop Offset="1"   Color="Yellow" />
                    </LinearGradientBrush>
                </Rectangle.Fill>
            </Rectangle>
            <DockPanel LastChildFill="False">
                <TextBlock Name="rectangleFirstTextBlock" DockPanel.Dock="Left"
                    Width="10"
                    Text="0" />
                <TextBlock Name="rectangleLastTextBlock" DockPanel.Dock="Right"
                    Text="{Binding ElementName=sourceRectangle, Path=ActualWidth}" />
            </DockPanel>
            <StackPanel Name="mousePointStackPanel"
                HorizontalAlignment="Center"
                Orientation="Horizontal"
                Visibility="Collapsed">
                <TextBlock Text="마우스 좌표 : " />
                <TextBlock Name="mousePointTextBlock" />
            </StackPanel>
            <Rectangle Name="targetRectangle"
                Margin="0 30 0 0"
                Width="50"
                Height="50"
                Stroke="Black"
                StrokeThickness="1">
                <Rectangle.Fill>
                    <SolidColorBrush x:Name="targetBrush"
                        Color="Blue" />
                </Rectangle.Fill>
            </Rectangle>
            <TextBlock Name="targetColorTextBlock"
                Margin="0 10 0 0"
                HorizontalAlignment="Center" />
        </StackPanel>
    </Grid>
</Window>

 

▶ MainWindow.xaml.cs

using System;
using System.Linq;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Shapes;

namespace TestProject
{
    /// <summary>
    /// 메인 윈도우
    /// </summary>
    public partial class MainWindow : Window
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 생성자 - MainWindow()

        /// <summary>
        /// 생성자
        /// </summary>
        public MainWindow()
        {
            InitializeComponent();

            this.sourceRectangle.MouseEnter += sourceRectangle_MouseEnter;
            this.sourceRectangle.MouseMove  += sourceRectangle_MouseMove;
            this.sourceRectangle.MouseLeave += sourceRectangle_MouseLeave;
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Private
        //////////////////////////////////////////////////////////////////////////////// Event

        #region 소스 사각형 마우스 진입시 처리하기 - sourceRectangle_MouseEnter(sender, e)

        /// <summary>
        /// 소스 사각형 마우스 진입시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void sourceRectangle_MouseEnter(object sender, MouseEventArgs e)
        {
            this.mousePointStackPanel.Visibility = Visibility.Visible;
        }

        #endregion
        #region 소스 사각형 마우스 이동시 처리하기 - sourceRectangle_MouseMove(sender, e)

        /// <summary>
        /// 소스 사각형 마우스 이동시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void sourceRectangle_MouseMove(object sender, MouseEventArgs e)
        {
            Rectangle rectangle = sender as Rectangle;

            Point mousePoint = e.GetPosition(rectangle);

            double rectangleWidth = rectangle.ActualWidth;

            double offset = mousePoint.X / rectangleWidth;

            Color color = GetColor(this.sourceBrush, offset);

            this.targetBrush.Color = color;

            this.targetColorTextBlock.Text = color.ToString();

            if(this.mousePointStackPanel.Visibility == Visibility.Visible)
            {
                this.mousePointTextBlock.Text = mousePoint.ToString();
            }
        }

        #endregion
        #region 소스 사각형 마우스 이탈시 처리하기 - sourceRectangle_MouseLeave(sender, e)

        /// <summary>
        /// 소스 사각형 마우스 이탈시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void sourceRectangle_MouseLeave(object sender, MouseEventArgs e)
        {
            this.mousePointStackPanel.Visibility = Visibility.Collapsed;

            this.targetBrush.Color = Colors.Transparent;

            this.targetColorTextBlock.Text = Colors.Transparent.ToString();
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////// Function

        #region 색상 구하기 - GetColor(brush, offset)

        /// <summary>
        /// 색상 구하기
        /// </summary>
        /// <param name="brush">선형 그라디언트 브러시</param>
        /// <param name="offset">오프셋</param>
        /// <returns>색상</returns>
        private Color GetColor(LinearGradientBrush brush, double offset)
        {
            GradientStopCollection gradientStopCollection = brush.GradientStops;

            GradientStop[] gradientStopArray = gradientStopCollection.OrderBy(x => x.Offset).ToArray();

            if(offset <= 0)
            {
                return gradientStopArray[0].Color;
            }

            if(offset >= 1)
            {
                return gradientStopArray[gradientStopArray.Length - 1].Color;
            }

            GradientStop firstStop = gradientStopArray[0];
            GradientStop lastStop  = null;

            foreach(GradientStop stop in gradientStopArray)
            {
                if(stop.Offset >= offset)
                {
                    lastStop = stop;

                    break;
                }

                firstStop = stop;
            }

            offset = Math.Round((offset - firstStop.Offset) / (lastStop.Offset - firstStop.Offset), 2);

            byte alpha = (byte)((lastStop.Color.A - firstStop.Color.A) * offset + firstStop.Color.A);
            byte red   = (byte)((lastStop.Color.R - firstStop.Color.R) * offset + firstStop.Color.R);
            byte green = (byte)((lastStop.Color.G - firstStop.Color.G) * offset + firstStop.Color.G);
            byte blue  = (byte)((lastStop.Color.B - firstStop.Color.B) * offset + firstStop.Color.B);

            return Color.FromArgb(alpha, red, green, blue);
        }

        #endregion
    }
}
728x90
반응형
Posted by 사용자 icodebroker
TAG , , , ,

댓글을 달아 주세요