From window, to wind

趣味のゲームとJavaプログラムなどについて 適当に書き綴るブログ

スポンサーサイト

上記の広告は1ヶ月以上更新のないブログに表示されています。
新しい記事を書く事で広告が消せます。

WPFでカラーピッカー作ったよ

C#とWPFでカラーピッカーを作ったのでソースコード置いておきます。
WPFは初心者なので色々四苦八苦して作ったのでいい勉強にはなりました。
カラーピッカーはユーザーコントロールで作っています。

ColorPallete.jpg

ツール上で色が必要な時はSolidColorBrushのnewBrushかそれぞれのスライダーの値からARGBの値を取得します。

ColorPallete.xaml (UserControl)
> TransparentButton.xaml (ResourceDictionary)
> PalleteButton.xaml (ResourceDictionary)
> TriSlider.xaml (ResourceDictionary)
> HSVColor.cs (class)

何故か私の開発環境だとWPFでマウス左ボタンのマウスイベントが取れなかったので、
透明なボタン(TransparentButton)を上に重ねてClickイベントで取得しています。

またデフォルトのSliderのデザインが気に入らなかったのでTriSliderとして作成しています。
同じ様な理由でマウスオーバー時の背景変化が気に入らないのでカラーパレットのボタンもPalleteButtonを作成しています。

色相スライダーの作成のためにRGBとHSVを変換するHSVColorクラスを作りました。
ColorPallete.xaml
<UserControl x:Class="DotSupporter.ColorPallete"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="430" d:DesignWidth="200">
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="TriSlider.xaml" />
<ResourceDictionary Source="TransparentButton.xaml"/>
<ResourceDictionary Source="PalleteButton.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
<Canvas>
<Rectangle Height="25" HorizontalAlignment="Left" Margin="19,143,0,0" VerticalAlignment="Top" Width="25">
<Rectangle.Fill>
<DrawingBrush Viewport="0,0,0.4,0.4" TileMode="Tile">
<DrawingBrush.Drawing>
<DrawingGroup>
<GeometryDrawing Brush="White">
<GeometryDrawing.Geometry>
<RectangleGeometry Rect="0,0,100,100" />
</GeometryDrawing.Geometry>
</GeometryDrawing>
<GeometryDrawing Brush="LightGray">
<GeometryDrawing.Geometry>
<GeometryGroup>
<RectangleGeometry Rect="0,0,50,50" />
<RectangleGeometry Rect="50,50,50,50" />
</GeometryGroup>
</GeometryDrawing.Geometry>
</GeometryDrawing>
</DrawingGroup>
</DrawingBrush.Drawing>
</DrawingBrush>
</Rectangle.Fill>
</Rectangle>
<Rectangle Height="14" HorizontalAlignment="Left" Margin="55,113,0,0" VerticalAlignment="Top" Width="90">
<Rectangle.Fill>
<DrawingBrush TileMode="Tile" Viewport="0,0,0.07,0.5">
<DrawingBrush.Drawing>
<DrawingGroup>
<GeometryDrawing Brush="White">
<GeometryDrawing.Geometry>
<RectangleGeometry Rect="0,0,100,100" />
</GeometryDrawing.Geometry>
</GeometryDrawing>
<GeometryDrawing Brush="LightGray">
<GeometryDrawing.Geometry>
<GeometryGroup>
<RectangleGeometry Rect="0,0,50,50" />
<RectangleGeometry Rect="50,50,50,50" />
</GeometryGroup>
</GeometryDrawing.Geometry>
</GeometryDrawing>
</DrawingGroup>
</DrawingBrush.Drawing>
</DrawingBrush>
</Rectangle.Fill>
</Rectangle>
<Slider Style="{StaticResource RTriSlider}" HorizontalAlignment="Left" Margin="50,17,0,0" Name="redslider" VerticalAlignment="Top" Width="100" Maximum="255" SmallChange="1" ValueChanged="RGBslider_ValueChanged" />
<TextBox HorizontalAlignment="Left" Margin="153,16,0,0" Name="redtextbox" VerticalAlignment="Top" Width="35" Text="{Binding ElementName=redslider, Path=Value, StringFormat=F0}" />
<Label Content="Red" HorizontalAlignment="Left" Margin="12,12,0,0" VerticalAlignment="Top" />
<TextBox HorizontalAlignment="Left" Margin="153,46,0,0" Name="greentextbox" Text="{Binding ElementName=greenslider, Path=Value, StringFormat=F0}" VerticalAlignment="Top" Width="35" />
<Slider Style="{StaticResource GTriSlider}" HorizontalAlignment="Left" Margin="50,48,0,0" Maximum="255" Name="greenslider" VerticalAlignment="Top" Width="100" ValueChanged="RGBslider_ValueChanged" />
<Label Content="Green" HorizontalAlignment="Left" Margin="0,43,0,0" VerticalAlignment="Top" />
<TextBox HorizontalAlignment="Left" Margin="153,76,0,0" Name="textBox1" Text="{Binding ElementName=blueslider, Path=Value, StringFormat=F0}" VerticalAlignment="Top" Width="35" />
<Slider Style="{StaticResource BTriSlider}" HorizontalAlignment="Left" Margin="50,78,0,0" Maximum="255" Name="blueslider" VerticalAlignment="Top" Width="100" ValueChanged="RGBslider_ValueChanged" />
<Label Content="Blue" HorizontalAlignment="Left" Margin="9,74,0,0" VerticalAlignment="Top" />
<TextBox HorizontalAlignment="Left" Margin="153,106,0,0" Name="arufatextBox" Text="{Binding ElementName=arufaslider, Path=Value, StringFormat=F0}" VerticalAlignment="Top" Width="35" />
<Slider Style="{StaticResource ArufaTriSlider}" HorizontalAlignment="Left" Margin="50,108,0,0" Maximum="255" Name="arufaslider" VerticalAlignment="Top" Width="100" Value="255" ValueChanged="RGBslider_ValueChanged" />
<Label Content="Arufa" HorizontalAlignment="Left" Margin="3,104,0,0" VerticalAlignment="Top" />
<Rectangle Height="25" HorizontalAlignment="Left" Margin="19,143,0,0" Name="colorsquare" Stroke="Black" VerticalAlignment="Top" Width="25" Fill="{DynamicResource RectBrush}" />
<Slider HorizontalAlignment="Left" Margin="50,144,0,0" Maximum="360" Name="hueslider" Style="{StaticResource HueTriSlider}" Value="0" VerticalAlignment="Top" Width="100" ValueChanged="Hueslider_ValueChanged" />
<TextBox HorizontalAlignment="Left" Margin="153,145,0,0" Name="huetextBox" Text="{Binding ElementName=hueslider, Path=Value, StringFormat=F0}" VerticalAlignment="Top" Width="35" />
<Image Height="128" HorizontalAlignment="Left" Margin="17,174,0,0" Name="SVImage" Stretch="Fill" VerticalAlignment="Top" Width="128" />
<Ellipse Height="11" HorizontalAlignment="Left" Name="SVpoint" Stroke="Black" Fill="White" VerticalAlignment="Top" Width="11" Canvas.Left="12" Canvas.Top="296" />
<Button Style="{StaticResource TransparentButton}" Height="128" HorizontalAlignment="Left" Name="SVButton" VerticalAlignment="Top" Width="128" Click="SVButton_Click" Canvas.Left="17" Canvas.Top="174" />
<CheckBox Canvas.Left="19" Canvas.Top="308" Content="パレットに保存する" Height="16" Name="PalleteCheckBox" />

<Button Style="{StaticResource PalleteButton}" Canvas.Left="12" Canvas.Top="330" Height="23" Width="23" Background="Black" Click="PalleteButton_Click" />
<Button Style="{StaticResource PalleteButton}" Canvas.Left="35" Canvas.Top="330" Height="23" Width="23" Background="Red" Click="PalleteButton_Click" />
<Button Style="{StaticResource PalleteButton}" Canvas.Left="58" Canvas.Top="330" Height="23" Width="23" Background="#FFFF6A00" Click="PalleteButton_Click" />
<Button Style="{StaticResource PalleteButton}" Canvas.Left="81" Canvas.Top="330" Height="23" Width="23" Background="#FFFFD800" Click="PalleteButton_Click" />
<Button Style="{StaticResource PalleteButton}" Canvas.Left="104" Canvas.Top="330" Height="23" Width="23" Background="#FFB6FF00" Click="PalleteButton_Click" />
<Button Style="{StaticResource PalleteButton}" Canvas.Left="127" Canvas.Top="330" Height="23" Width="23" Background="#FF4CFF00" Click="PalleteButton_Click" />
<Button Style="{StaticResource PalleteButton}" Canvas.Left="150" Canvas.Top="330" Height="23" Width="23" Background="#FF00FF21" Click="PalleteButton_Click" />
<Button Style="{StaticResource PalleteButton}" Canvas.Left="173" Canvas.Top="330" Height="23" Width="23" Background="#FF00FF90" Click="PalleteButton_Click" />

<Button Style="{StaticResource PalleteButton}" Canvas.Left="12" Canvas.Top="353" Height="23" Width="23" Background="White" Click="PalleteButton_Click" />
<Button Style="{StaticResource PalleteButton}" Canvas.Left="35" Canvas.Top="353" Height="23" Width="23" Background="#FFFF006E" Click="PalleteButton_Click" />
<Button Style="{StaticResource PalleteButton}" Canvas.Left="58" Canvas.Top="353" Height="23" Width="23" Background="#FFFF00DC" Click="PalleteButton_Click" />
<Button Style="{StaticResource PalleteButton}" Canvas.Left="81" Canvas.Top="353" Height="23" Width="23" Background="#FFB200FF" Click="PalleteButton_Click" />
<Button Style="{StaticResource PalleteButton}" Canvas.Left="104" Canvas.Top="353" Height="23" Width="23" Background="#FF4800FF" Click="PalleteButton_Click" />
<Button Style="{StaticResource PalleteButton}" Canvas.Left="127" Canvas.Top="353" Height="23" Width="23" Background="#FF0026FF" Click="PalleteButton_Click" />
<Button Style="{StaticResource PalleteButton}" Canvas.Left="150" Canvas.Top="353" Height="23" Width="23" Background="#FF0090FF" Click="PalleteButton_Click" />
<Button Style="{StaticResource PalleteButton}" Canvas.Left="173" Canvas.Top="353" Height="23" Width="23" Background="Cyan" Click="PalleteButton_Click" />

<Button Style="{StaticResource PalleteButton}" Canvas.Left="12" Canvas.Top="376" Height="23" Width="23" Background="White" Click="PalleteButton_Click" />
<Button Style="{StaticResource PalleteButton}" Canvas.Left="35" Canvas.Top="376" Height="23" Width="23" Background="White" Click="PalleteButton_Click" />
<Button Style="{StaticResource PalleteButton}" Canvas.Left="58" Canvas.Top="376" Height="23" Width="23" Background="White" Click="PalleteButton_Click" />
<Button Style="{StaticResource PalleteButton}" Canvas.Left="81" Canvas.Top="376" Height="23" Width="23" Background="White" Click="PalleteButton_Click" />
<Button Style="{StaticResource PalleteButton}" Canvas.Left="104" Canvas.Top="376" Height="23" Width="23" Background="White" Click="PalleteButton_Click" />
<Button Style="{StaticResource PalleteButton}" Canvas.Left="127" Canvas.Top="376" Height="23" Width="23" Background="white" Click="PalleteButton_Click" />
<Button Style="{StaticResource PalleteButton}" Canvas.Left="150" Canvas.Top="376" Height="23" Width="23" Background="white" Click="PalleteButton_Click" />
<Button Style="{StaticResource PalleteButton}" Canvas.Left="173" Canvas.Top="376" Height="23" Width="23" Background="white" Click="PalleteButton_Click" />

<Button Style="{StaticResource PalleteButton}" Canvas.Left="12" Canvas.Top="399" Height="23" Width="23" Background="White" Click="PalleteButton_Click" />
<Button Style="{StaticResource PalleteButton}" Canvas.Left="35" Canvas.Top="399" Height="23" Width="23" Background="White" Click="PalleteButton_Click" />
<Button Style="{StaticResource PalleteButton}" Canvas.Left="58" Canvas.Top="399" Height="23" Width="23" Background="White" Click="PalleteButton_Click" />
<Button Style="{StaticResource PalleteButton}" Canvas.Left="81" Canvas.Top="399" Height="23" Width="23" Background="White" Click="PalleteButton_Click" />
<Button Style="{StaticResource PalleteButton}" Canvas.Left="104" Canvas.Top="399" Height="23" Width="23" Background="White" Click="PalleteButton_Click" />
<Button Style="{StaticResource PalleteButton}" Canvas.Left="127" Canvas.Top="399" Height="23" Width="23" Background="white" Click="PalleteButton_Click" />
<Button Style="{StaticResource PalleteButton}" Canvas.Left="150" Canvas.Top="399" Height="23" Width="23" Background="white" Click="PalleteButton_Click" />
<Button Style="{StaticResource PalleteButton}" Canvas.Left="173" Canvas.Top="399" Height="23" Width="23" Background="white" Click="PalleteButton_Click" />
</Canvas>
</UserControl>



ColorPallete.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace DotSupporter {
/// <summary>
/// ColorPallete.xaml の相互作用ロジック
/// </summary>
public partial class ColorPallete : UserControl {

public Brush newBrush = Brushes.Black;
private LinearGradientBrush newRectBrush;
public Color newColor;
private LinearGradientBrush newRgradBrush, newGgradBrush, newBgradBrush, newArufagradBrush;
private WriteableBitmap SVbitmap;
private int[] HSV = new int[] { 0, 0, 0 };
private byte[] bgra = new byte[] { 0, 0, 0, 255 };
private bool RGBsliderChange = false;
private bool HuesliderChange = false;
private bool SVButtonClick = false;
private bool initializekey = false;

public ColorPallete() {
InitializeComponent();
initializekey = true;
createSVImage();
}

private void createSVImage() {
SVbitmap = new WriteableBitmap((int)SVImage.Width, (int)SVImage.Height, 96, 96,
PixelFormats.Bgra32, null);
byte[] pxdata = new byte[SVbitmap.PixelHeight * SVbitmap.PixelWidth * 4];
int stride = SVbitmap.PixelWidth * ((SVbitmap.Format.BitsPerPixel + 7) / 8);
//縦V横S
for (int y = 0; y < SVImage.Height; y++) {
for (int x = 0; x < SVImage.Width; x++) {
int S = (int)(x * 255 / SVImage.Width);
int V = (int)((SVImage.Height - y) * 255 / SVImage.Height);
byte[] rgb = new byte[3];
rgb = HSVColor.FromHSV(HSV[0], S, V);
pxdata[4 * (y * SVbitmap.PixelWidth + x) + 2] = rgb[0];//red
pxdata[4 * (y * SVbitmap.PixelWidth + x) + 1] = rgb[1];//green
pxdata[4 * (y * SVbitmap.PixelWidth + x) + 0] = rgb[2];//blue
pxdata[4 * (y * SVbitmap.PixelWidth + x) + 3] = 255;//arufa
}
}
SVbitmap.WritePixels(new Int32Rect(0, 0, SVbitmap.PixelWidth, SVbitmap.PixelHeight), pxdata, stride, 0);
SVImage.Source = SVbitmap;
}

private void Hueslider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e) {
HuesliderChange = true;

if (initializekey) {
if (!RGBsliderChange && !SVButtonClick) {
HSV[0] = (int)hueslider.Value;
byte[] rgb = HSVColor.FromHSV(HSV[0], HSV[1], HSV[2]);
bgra[0] = rgb[2];
bgra[1] = rgb[1];
bgra[2] = rgb[0];
redslider.Value = rgb[0];
greenslider.Value = rgb[1];
blueslider.Value = rgb[2];
}
}
createSVImage();
HuesliderChange = false;
}


private void RGBslider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e) {
RGBsliderChange = true;


if (initializekey) {
if (!HuesliderChange && !SVButtonClick) {
bgra[3] = (byte)arufaslider.Value;
bgra[2] = (byte)redslider.Value;
bgra[1] = (byte)greenslider.Value;
bgra[0] = (byte)blueslider.Value;
HSV = HSVColor.makeHSV(bgra[2], bgra[1], bgra[0]);
hueslider.Value = HSV[0];

int x = (int)(HSV[1] * SVImage.Width / 255) + int.Parse(SVButton.GetValue(Canvas.LeftProperty).ToString());
int y = (int)(SVImage.Height * (1 - HSV[2] / 255.0)) + int.Parse(SVButton.GetValue(Canvas.TopProperty).ToString());

//int x = (int)(HSV[1] * SVImage.Width / 255)+17;
//int y = (int)(SVImage.Height * (1 - HSV[2] / 255.0))+174;

SVpoint.SetValue(Canvas.TopProperty, y - 5.0);//174
SVpoint.SetValue(Canvas.LeftProperty, x - 5.0);//17
}
}

newColor = Color.FromArgb(bgra[3], bgra[2], bgra[1], bgra[0]);
newBrush = new SolidColorBrush(newColor);

newRectBrush = new LinearGradientBrush();
newRectBrush.GradientStops.Add(new GradientStop(Color.FromArgb(255, bgra[2], bgra[1], bgra[0]), 0.0));
newRectBrush.GradientStops.Add(new GradientStop(Color.FromArgb(bgra[3], bgra[2], bgra[1], bgra[0]), 1.0));
this.Resources["RectBrush"] = newRectBrush;

newRgradBrush = new LinearGradientBrush();
newRgradBrush.GradientStops.Add(new GradientStop(Color.FromArgb(255, 0, bgra[1], bgra[0]), 0.0));
newRgradBrush.GradientStops.Add(new GradientStop(Color.FromArgb(255, 255, bgra[1], bgra[0]), 1.0));
this.Resources["rGradBrush"] = newRgradBrush;

newGgradBrush = new LinearGradientBrush();
newGgradBrush.GradientStops.Add(new GradientStop(Color.FromArgb(255, bgra[2], 0, bgra[0]), 0.0));
newGgradBrush.GradientStops.Add(new GradientStop(Color.FromArgb(255, bgra[2], 255, bgra[0]), 1.0));
this.Resources["gGradBrush"] = newGgradBrush;

newBgradBrush = new LinearGradientBrush();
newBgradBrush.GradientStops.Add(new GradientStop(Color.FromArgb(255, bgra[2], bgra[1], 0), 0.0));
newBgradBrush.GradientStops.Add(new GradientStop(Color.FromArgb(255, bgra[2], bgra[1], 255), 1.0));
this.Resources["bGradBrush"] = newBgradBrush;

newArufagradBrush = new LinearGradientBrush();
newArufagradBrush.GradientStops.Add(new GradientStop(Color.FromArgb(0, bgra[2], bgra[1], bgra[0]), 0.0));
newArufagradBrush.GradientStops.Add(new GradientStop(Color.FromArgb(255, bgra[2], bgra[1], bgra[0]), 1.0));
this.Resources["arufaGradBrush"] = newArufagradBrush;


RGBsliderChange = false;
}

private void SVButton_Click(object sender, RoutedEventArgs e) {
Point MPoint = Mouse.GetPosition((IInputElement)sender);
Point setPoint = Mouse.GetPosition(this);

SVpoint.SetValue(Canvas.TopProperty, setPoint.Y - 5);
SVpoint.SetValue(Canvas.LeftProperty, setPoint.X - 5);

SVButtonClick = true;
if (!RGBsliderChange) {
//縦V横S
HSV[1] = (int)(MPoint.X * 255 / SVImage.Width);
HSV[2] = (int)((SVImage.Height - MPoint.Y) * 255 / SVImage.Height);
byte[] rgb = HSVColor.FromHSV(HSV[0], HSV[1], HSV[2]);
bgra[0] = rgb[2];
bgra[1] = rgb[1];
bgra[2] = rgb[0];
redslider.Value = rgb[0];
greenslider.Value = rgb[1];
blueslider.Value = rgb[2];
}
SVButtonClick = false;
}

private void PalleteButton_Click(object sender, RoutedEventArgs e) {
if ((bool)PalleteCheckBox.IsChecked) {
//パレットに保存する
((Button)sender).Background = newBrush;
} else {
//パレットから取得する
SolidColorBrush getBrush = (SolidColorBrush)((Button)sender).Background;
redslider.Value = getBrush.Color.R;
greenslider.Value = getBrush.Color.G;
blueslider.Value = getBrush.Color.B;
arufaslider.Value = getBrush.Color.A;
}
}
}
}



TransparentButton.xaml
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style x:Key="TransparentButton" TargetType="{x:Type Button}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Grid>
<Border Background="Transparent" />
<Rectangle Stroke="Black" StrokeThickness="1" />
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsPressed" Value="true" />
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>



PalleteButton.xaml
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style x:Key="PalleteButton" TargetType="{x:Type Button}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Grid>
<Rectangle Name="rect" RadiusX="3" RadiusY="3" Fill="{TemplateBinding Button.Background}" StrokeThickness="1">
<Rectangle.Stroke>
<LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
<GradientStop Color="#000000" Offset="0"/>
<GradientStop Color="#000000" Offset="0.5"/>
<GradientStop Color="#888888" Offset="1"/>
</LinearGradientBrush>
</Rectangle.Stroke>
</Rectangle>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="true" >
<Setter TargetName="rect" Property="Stroke">
<Setter.Value>
<LinearGradientBrush StartPoint="0,0" EndPoint="0,1" >
<GradientStop Color="#00aaff" Offset="0"/>
<GradientStop Color="#00aaff" Offset="0.5"/>
<GradientStop Color="#99ccff" Offset="1"/>
</LinearGradientBrush>
</Setter.Value>
</Setter>
</Trigger>
<Trigger Property="IsPressed" Value="true" >
<Setter TargetName="rect" Property="Stroke">
<Setter.Value>
<LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
<GradientStop Color="#99ccff" Offset="0"/>
<GradientStop Color="#99ccff" Offset="0.5"/>
<GradientStop Color="#00aaff" Offset="1"/>
</LinearGradientBrush>
</Setter.Value>
</Setter>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>



TriSlider.xaml
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<LinearGradientBrush x:Key="RectBrush">
<GradientStop Color="Black" Offset="0" />
<GradientStop Color="Black" Offset="1" />
</LinearGradientBrush>
<LinearGradientBrush x:Key="rGradBrush">
<GradientStop Color="Black" Offset="0" />
<GradientStop Color="Red" Offset="1" />
</LinearGradientBrush>
<LinearGradientBrush x:Key="gGradBrush">
<GradientStop Color="Black" Offset="0" />
<GradientStop Color="Lime" Offset="1" />
</LinearGradientBrush>
<LinearGradientBrush x:Key="bGradBrush">
<GradientStop Color="Black" Offset="0" />
<GradientStop Color="Blue" Offset="1" />
</LinearGradientBrush>
<LinearGradientBrush x:Key="arufaGradBrush">
<GradientStop Color="#00000000" Offset="0" />
<GradientStop Color="Black" Offset="1" />
</LinearGradientBrush>
<LinearGradientBrush x:Key="hueGradBrush">
<GradientStop Color="#FF0000" Offset="0" />
<GradientStop Color="#FFFF00" Offset="0.1666666" />
<GradientStop Color="#00FF00" Offset="0.3333333" />
<GradientStop Color="#00FFFF" Offset="0.5" />
<GradientStop Color="#0000FF" Offset="0.6666666" />
<GradientStop Color="#FF00FF" Offset="0.8333333" />
<GradientStop Color="#FF0000" Offset="1" />
</LinearGradientBrush>
<Style x:Key="SliderButtonStyle"
TargetType="{x:Type RepeatButton}">
<Setter Property="SnapsToDevicePixels"
Value="true" />
<Setter Property="OverridesDefaultStyle"
Value="true" />
<Setter Property="IsTabStop"
Value="false" />
<Setter Property="Focusable"
Value="false" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type RepeatButton}">
<Border Background="Transparent" />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="TriThumb" TargetType="{x:Type Thumb}">
<Setter Property="OverridesDefaultStyle" Value="True"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Thumb}">
<Polygon Stroke="Black" StrokeThickness="1" Points="5,15 10,23 0,23" >
<Polygon.Fill>
<SolidColorBrush Color="Black"/>
</Polygon.Fill>
</Polygon>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="ArufaTriSlider" TargetType="{x:Type Slider}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Slider}">
<Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto" MinHeight="{TemplateBinding MinHeight}"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Rectangle Fill="{DynamicResource arufaGradBrush}" Grid.Row="1" Height="14" Width="90" Stroke="Black" StrokeThickness="1"/>
<TickBar x:Name="TopTick" Visibility="Collapsed" Fill="{TemplateBinding Foreground}" Placement="Top" Height="4" Grid.Row="0"/>
<TickBar x:Name="BottomTick" Visibility="Collapsed" Fill="{TemplateBinding Foreground}" Placement="Bottom" Height="4" Grid.Row="0"/>
<Border x:Name="TrackBackground" BorderThickness="1" CornerRadius="1" Margin="5,0" VerticalAlignment="Center" Height="4.0" Grid.Row="1" >
<Canvas Margin="-6,-1">
<Rectangle Visibility="Hidden" x:Name="PART_SelectionRange" Height="4.0" Fill="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}" Stroke="{DynamicResource {x:Static SystemColors.ControlDarkDarkBrushKey}}" StrokeThickness="1.0"/>
</Canvas>
</Border>
<Track x:Name="PART_Track" Grid.Row="1">
<Track.DecreaseRepeatButton>
<RepeatButton Style="{StaticResource SliderButtonStyle}" Command="{x:Static Slider.DecreaseLarge}"/>
</Track.DecreaseRepeatButton>
<Track.IncreaseRepeatButton>
<RepeatButton Style="{StaticResource SliderButtonStyle}" Command="{x:Static Slider.IncreaseLarge}"/>
</Track.IncreaseRepeatButton>
<Track.Thumb>
<Thumb x:Name="Thumb" Height="23" Background="Black" Style="{StaticResource TriThumb}" />
</Track.Thumb>
</Track>
</Grid>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="RTriSlider" TargetType="{x:Type Slider}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Slider}">
<Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto" MinHeight="{TemplateBinding MinHeight}"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Rectangle Fill="{DynamicResource rGradBrush}" Grid.Row="1" Height="14" Width="90" Stroke="Black" StrokeThickness="1"/>
<TickBar x:Name="TopTick" Visibility="Collapsed" Fill="{TemplateBinding Foreground}" Placement="Top" Height="4" Grid.Row="0"/>
<TickBar x:Name="BottomTick" Visibility="Collapsed" Fill="{TemplateBinding Foreground}" Placement="Bottom" Height="4" Grid.Row="0"/>
<Border x:Name="TrackBackground" BorderThickness="1" CornerRadius="1" Margin="5,0" VerticalAlignment="Center" Height="4.0" Grid.Row="1" >
<Canvas Margin="-6,-1">
<Rectangle Visibility="Hidden" x:Name="PART_SelectionRange" Height="4.0" Fill="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}" Stroke="{DynamicResource {x:Static SystemColors.ControlDarkDarkBrushKey}}" StrokeThickness="1.0"/>
</Canvas>
</Border>
<Track x:Name="PART_Track" Grid.Row="1">
<Track.DecreaseRepeatButton>
<RepeatButton Style="{StaticResource SliderButtonStyle}" Command="{x:Static Slider.DecreaseLarge}"/>
</Track.DecreaseRepeatButton>
<Track.IncreaseRepeatButton>
<RepeatButton Style="{StaticResource SliderButtonStyle}" Command="{x:Static Slider.IncreaseLarge}"/>
</Track.IncreaseRepeatButton>
<Track.Thumb>
<Thumb x:Name="Thumb" Height="23" Background="Black" Style="{StaticResource TriThumb}" />
</Track.Thumb>
</Track>
</Grid>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="GTriSlider" TargetType="{x:Type Slider}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Slider}">
<Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto" MinHeight="{TemplateBinding MinHeight}"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Rectangle Fill="{DynamicResource gGradBrush}" Grid.Row="1" Height="14" Width="90" Stroke="Black" StrokeThickness="1"/>
<TickBar x:Name="TopTick" Visibility="Collapsed" Fill="{TemplateBinding Foreground}" Placement="Top" Height="4" Grid.Row="0"/>
<TickBar x:Name="BottomTick" Visibility="Collapsed" Fill="{TemplateBinding Foreground}" Placement="Bottom" Height="4" Grid.Row="0"/>
<Border x:Name="TrackBackground" BorderThickness="1" CornerRadius="1" Margin="5,0" VerticalAlignment="Center" Height="4.0" Grid.Row="1" >
<Canvas Margin="-6,-1">
<Rectangle Visibility="Hidden" x:Name="PART_SelectionRange" Height="4.0" Fill="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}" Stroke="{DynamicResource {x:Static SystemColors.ControlDarkDarkBrushKey}}" StrokeThickness="1.0"/>
</Canvas>
</Border>
<Track x:Name="PART_Track" Grid.Row="1">
<Track.DecreaseRepeatButton>
<RepeatButton Style="{StaticResource SliderButtonStyle}" Command="{x:Static Slider.DecreaseLarge}"/>
</Track.DecreaseRepeatButton>
<Track.IncreaseRepeatButton>
<RepeatButton Style="{StaticResource SliderButtonStyle}" Command="{x:Static Slider.IncreaseLarge}"/>
</Track.IncreaseRepeatButton>
<Track.Thumb>
<Thumb x:Name="Thumb" Height="23" Background="Black" Style="{StaticResource TriThumb}" />
</Track.Thumb>
</Track>
</Grid>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="BTriSlider" TargetType="{x:Type Slider}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Slider}">
<Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto" MinHeight="{TemplateBinding MinHeight}"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Rectangle Fill="{DynamicResource bGradBrush}" Grid.Row="1" Height="14" Width="90" Stroke="Black" StrokeThickness="1"/>
<TickBar x:Name="TopTick" Visibility="Collapsed" Fill="{TemplateBinding Foreground}" Placement="Top" Height="4" Grid.Row="0"/>
<TickBar x:Name="BottomTick" Visibility="Collapsed" Fill="{TemplateBinding Foreground}" Placement="Bottom" Height="4" Grid.Row="0"/>
<Border x:Name="TrackBackground" BorderThickness="1" CornerRadius="1" Margin="5,0" VerticalAlignment="Center" Height="4.0" Grid.Row="1" >
<Canvas Margin="-6,-1">
<Rectangle Visibility="Hidden" x:Name="PART_SelectionRange" Height="4.0" Fill="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}" Stroke="{DynamicResource {x:Static SystemColors.ControlDarkDarkBrushKey}}" StrokeThickness="1.0"/>
</Canvas>
</Border>
<Track x:Name="PART_Track" Grid.Row="1">
<Track.DecreaseRepeatButton>
<RepeatButton Style="{StaticResource SliderButtonStyle}" Command="{x:Static Slider.DecreaseLarge}"/>
</Track.DecreaseRepeatButton>
<Track.IncreaseRepeatButton>
<RepeatButton Style="{StaticResource SliderButtonStyle}" Command="{x:Static Slider.IncreaseLarge}"/>
</Track.IncreaseRepeatButton>
<Track.Thumb>
<Thumb x:Name="Thumb" Height="23" Background="Black" Style="{StaticResource TriThumb}" />
</Track.Thumb>
</Track>
</Grid>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="HueTriSlider" TargetType="{x:Type Slider}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Slider}">
<Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto" MinHeight="{TemplateBinding MinHeight}"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Rectangle Fill="{DynamicResource hueGradBrush}" Grid.Row="1" Height="14" Width="90" Stroke="Black" StrokeThickness="1"/>
<TickBar x:Name="TopTick" Visibility="Collapsed" Fill="{TemplateBinding Foreground}" Placement="Top" Height="4" Grid.Row="0"/>
<TickBar x:Name="BottomTick" Visibility="Collapsed" Fill="{TemplateBinding Foreground}" Placement="Bottom" Height="4" Grid.Row="0"/>
<Border x:Name="TrackBackground" BorderThickness="1" CornerRadius="1" Margin="5,0" VerticalAlignment="Center" Height="4.0" Grid.Row="1" >
<Canvas Margin="-6,-1">
<Rectangle Visibility="Hidden" x:Name="PART_SelectionRange" Height="4.0" Fill="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}" Stroke="{DynamicResource {x:Static SystemColors.ControlDarkDarkBrushKey}}" StrokeThickness="1.0"/>
</Canvas>
</Border>
<Track x:Name="PART_Track" Grid.Row="1">
<Track.DecreaseRepeatButton>
<RepeatButton Style="{StaticResource SliderButtonStyle}" Command="{x:Static Slider.DecreaseLarge}"/>
</Track.DecreaseRepeatButton>
<Track.IncreaseRepeatButton>
<RepeatButton Style="{StaticResource SliderButtonStyle}" Command="{x:Static Slider.IncreaseLarge}"/>
</Track.IncreaseRepeatButton>
<Track.Thumb>
<Thumb x:Name="Thumb" Height="23" Background="Black" Style="{StaticResource TriThumb}" />
</Track.Thumb>
</Track>
</Grid>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>



HSVColor.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Media;

namespace DotSupporter {
class HSVColor {


public static int[] makeHSV(byte r, byte g, byte b) {
//RGBからHSVへの変換
int[] HSV=new int[3];

double max = Math.Max(Math.Max(r, g), b);
double min = Math.Min(Math.Min(r, g), b);
if (max == min) {
HSV[0] = 0;
} else if (max == r) {
HSV[0] = (int)(60.0 * (g - b) / (max - min));
} else if (max == g) {
HSV[0] = (int)(60.0 * (2 + (b - r) / (max - min)));
} else {
HSV[0] = (int)(60.0 * (4 + (r - g) / (max - min)));
}

if (HSV[0] < 0) HSV[0] += 360;

if (max == 0) {
HSV[1] = 0;
} else {
HSV[1] = (int)(255.0 * (max - min) / max);
}
HSV[2] = (int)max;

return (HSV);
}

public static byte[] FromHSV(int H, int S, int V) {
//HSVからRGBへの変換
byte[] rgb = new byte[3];

if (H < 0) H += 360;
if (H >= 360) H -= 360;
if (S < 0) S = 0;
if (S > 255) S = 255;
if (V < 0) V = 0;
if (V > 255) V = 255;

//RGBへの変換
int Hi = (int)Math.Floor((double)H / 60.0);
double F = ((double)H - 60.0 * Hi) / 60.0;
double P = V * (1 - (double)S / 255.0);
double Q = V * (1 - (double)S / 255.0 * F);
double T = V * (1 - (double)S / 255.0 * (1 - F));

switch (Hi) {
case 0:
rgb[0] = (byte)V;
rgb[1] = (byte)T;
rgb[2] = (byte)P;
break;
case 1:
rgb[0] = (byte)Q;
rgb[1] = (byte)V;
rgb[2] = (byte)P;
break;
case 2:
rgb[0] = (byte)P;
rgb[1] = (byte)V;
rgb[2] = (byte)T;
break;
case 3:
rgb[0] = (byte)P;
rgb[1] = (byte)Q;
rgb[2] = (byte)V;
break;
case 4:
rgb[0] = (byte)T;
rgb[1] = (byte)P;
rgb[2] = (byte)V;
break;
case 5:
rgb[0] = (byte)V;
rgb[1] = (byte)P;
rgb[2] = (byte)Q;
break;
}
return (rgb);
}
}

}




無駄が多く冗長なコードになってすいません。
WPFはまだまだ初心者ですので、
このコードはこうした方がいいとかありましたら、
コメントなどで教えていただけると大変助かります。
スポンサーサイト

コメント

コメントの投稿


管理者にだけ表示を許可する

トラックバック

トラックバックURLはこちら
http://kagenyan.blog69.fc2.com/tb.php/139-d5f7952e
この記事にトラックバックする(FC2ブログユーザー)

FC2Ad

上記広告は1ヶ月以上更新のないブログに表示されています。新しい記事を書くことで広告を消せます。