■ LinearGradientBrush 클래스 : 오프셋 값을 사용해 색상 구하기
------------------------------------------------------------------------------------------------------------------------
▶ 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 } }
|
------------------------------------------------------------------------------------------------------------------------
'C# > WPF' 카테고리의 다른 글
[C#/WPF] 확대/축소/이동/드래그 가능한 캔버스 만들기 (0) | 2020.11.15 |
---|---|
[C#/WPF] DispatcherTimer 클래스 : 원 형태으로 이동시키기 (0) | 2020.11.15 |
[C#/WPF] DispatcherTimer 클래스 : 파동 형태로 이동시키기 (0) | 2020.11.15 |
[C#/WPF] DispatcherTimer 클래스 : 사각형을 부드럽게 이동시키기 (0) | 2020.11.15 |
[C#/WPF] Control 엘리먼트 : Background 속성에서 null과 Transparent 값 설정시 차이점 비교하기 (0) | 2020.11.06 |
[C#/WPF] LinearGradientBrush 클래스 : 오프셋 값을 사용해 색상 구하기 (0) | 2020.10.28 |
[C#/WPF] 누겟 설치 : FFME.Windows (0) | 2020.10.25 |
[C#/WPF] 커스텀 윈도우 크롬 사용하기 (0) | 2020.10.13 |
[C#/WPF] 파노라마 뷰 사용하기 (0) | 2020.09.24 |
[C#/WPF] PolyBezierSegment 클래스 : 1개 이상의 연결되는 베지어 곡선(Quadratic Bezier Curve) 그리기 (0) | 2020.09.23 |
[C#/WPF] PolyQuadraticBezierSegment 클래스 : 1개 이상의 연결되는 2차 베지어 곡선(Quadratic Bezier Curve) 그리기 (0) | 2020.09.23 |
댓글을 달아 주세요