Hue滑块采用HSL颜色等级

时间:2017-08-30 23:33:27

标签: c# wpf

我正在尝试使用值0-1制作Hue滑块。

调整滑块时,应更改矩形的颜色。

它适用于Luminence和Saturation,但不适用于Hue。它始终保持相同的颜色。

色相

Hue Slider

Luminence

Lum Slider

我正在使用此HSL颜色类

https://richnewman.wordpress.com/about/code-listings-and-diagrams/hslcolor-class/

项目来源

https://www.dropbox.com/s/fzj2kd01ei933y0/HueSlider.zip?dl=0

C#

public MainWindow()
{
    InitializeComponent();

    baseColor = new System.Drawing.Color();
    baseColor = System.Drawing.Color.FromArgb(255, 255, 0, 0); //red
}


System.Drawing.Color baseColor;
System.Drawing.Color newColor;


// Hue Slider
private void slHue_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
    // Change Hue
    newColor = Hue(baseColor, (double)slHue.Value);

    // Fill Rectangle
    Color brushColor = Color.FromArgb(newColor.A, newColor.R, newColor.G, newColor.B);
    Brush brush = new SolidColorBrush(brushColor);
    rectangle.Fill = brush;

    // Display Values
    tbxHue.Text = slHue.Value.ToString();
    tbxRGB.Text = newColor.ToString();
}


// Change Hue
public System.Drawing.Color Hue(System.Drawing.Color color, double hue)
{
    HSLColor hslColor = new HSLColor(color);
    hslColor.Hue *= hue; // 0 to 1

    return hslColor;
}

XAML

<Slider x:Name="slHue" 
        HorizontalAlignment="Left" 
        Margin="114,72,0,0" 
        VerticalAlignment="Top"
        Width="99" 
        Minimum="0"
        Maximum="1"
        Value="0"
        ValueChanged="slHue_ValueChanged"/>

<Rectangle x:Name="rectangle" 
           HorizontalAlignment="Left" 
           Height="100" 
           Margin="29,31,0,0" 
           Stroke="Black" 
           VerticalAlignment="Top" 
           Width="100">
</Rectangle>

1 个答案:

答案 0 :(得分:0)

我发现答案是使用这样的方法:

将色调设置为乘以240,或将滑块设置为使用0-240,饱和度设置为完全,亮度设置为一半。

public System.Drawing.Color Hue(System.Drawing.Color color, double value)
{
    HSLColor hslColor = new HSLColor(hue: value * 240, saturation: 240, luminosity: 120);

    return hslColor;
}

Hue 1