2016-06-30 12 views

Antwort

0

Dies ist kein Standard unterstützt in der UWP-Plattform, aber es ist einfach zu tun, nur mit Mathe;) Ich habe eine Helfer-Klasse, um genau das zu tun. Es gibt auch eine zusätzliche Klasse zum Definieren der HSV-Struktur. Der Trick besteht darin, den Farbton, die Sättigung und die Helligkeit (oder den Wert) der Farbe zu erhalten und dann die Helligkeit (den Wert) zu ändern.

Rufen Sie in der folgenden Klasse die Methode GetColors auf, um eine Liste der Schattierungen für diese Farbe zu erhalten.

public class ColorHelper 
{ 
    public static List<Windows.UI.Color> GetColors(Windows.UI.Color baseColor, int max) 
    { 
        // fill color shades list 
        List<Windows.UI.Color> colorShades = new List<Windows.UI.Color>(); 
        HSVColor hsv = ColorHelper.RGBtoHSV(baseColor); 
        hsv.V = 255; // alway use highest brightness to determine collection of shades 
        double v = hsv.V / max; 
        for (int i = 0; i < max; i++) 
        { 
            hsv.V = v * i; 
            if (hsv.V > 255) hsv.V = 255; 
            colorShades.Add(ColorHelper.HSVtoRGB(hsv)); 
        } 
        return colorShades; 
    } 

    public static HSVColor RGBtoHSV(Windows.UI.Color rgb) 
    { 
        double max, min, chroma; 
        HSVColor hsv = new HSVColor(); 

        min = Math.Min(Math.Min(rgb.R, rgb.G), rgb.B); 
        max = Math.Max(Math.Max(rgb.R, rgb.G), rgb.B); 
        chroma = max - min; 

        if (chroma != 0) 
        { 
            if (rgb.R == max) 
            { 
                hsv.H = (rgb.G - rgb.B) / chroma; 
                if (hsv.H < 0.0) hsv.H += 6.0; 
            } 
            else if (rgb.G == max) 
            { 
                hsv.H = ((rgb.B - rgb.R) / chroma) + 2.0; 
            } 
            else 
            { 
                hsv.H = ((rgb.R - rgb.G) / chroma) + 4.0; 
            } 
            hsv.H *= 60.0; 
            hsv.S = chroma / max; 
        } 

        hsv.V = max; 
        hsv.A = rgb.A; 

        return hsv; 
    } 

    public static Windows.UI.Color HSVtoRGB(HSVColor hsv) 
    { 
        double min, chroma, hdash, x; 
        Windows.UI.Color rgb = new Windows.UI.Color(); 

        chroma = hsv.S * hsv.V; 
        hdash = hsv.H / 60.0; 
        x = chroma * (1.0 - Math.Abs((hdash % 2.0) - 1.0)); 

        if (hdash < 1.0) 
        { 
            rgb.R = (byte)chroma; 
            rgb.G = (byte)x; 
        } 
        else if (hdash < 2.0) 
        { 
            rgb.R = (byte)x; 
            rgb.G = (byte)chroma; 
        } 
        else if (hdash < 3.0) 
        { 
            rgb.G = (byte)chroma; 
            rgb.B = (byte)x; 
        } 
        else if (hdash < 4.0) 
        { 
            rgb.G = (byte)x; 
            rgb.B = (byte)chroma; 
        } 
        else if (hdash < 5.0) 
        { 
            rgb.R = (byte)x; 
            rgb.B = (byte)chroma; 
        } 
        else if (hdash < 6.0) 
        { 
            rgb.R = (byte)chroma; 
            rgb.B = (byte)x; 
        } 

        min = hsv.V - chroma; 

        rgb.R += (byte)min; 
        rgb.G += (byte)min; 
        rgb.B += (byte)min; 
        rgb.A = (byte)hsv.A; 

        return rgb; 
    } 
} 

public class HSVColor 
{ 
    public double H { get; set; } 
    public double S { get; set; } 
    public double V { get; set; } 
    public double A { get; set; } 

    public HSVColor() 
    { 
        H = S = V = A = 1.0; 
    } 
} 
0

Sie können einen LinearGradientBrush oder einen RadialGradientBrush verwenden. Sie sollten nur zwei GradientStops hinzufügen, um einen Bereich anzuzeigen. Legen Sie den Hintergrund eines Elements fest, das mit dem Farbverlaufsbürsten gemalt werden soll.

<Page 
x:Class="CarServiceLogger.MainPage" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:local="using:CarServiceLogger" 
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
mc:Ignorable="d"> 
<!--{ThemeResource ApplicationPageBackgroundThemeBrush}--> 
<Grid > 
    <Grid.Background> 
     <LinearGradientBrush StartPoint="0,0" EndPoint="1,0"> 
      <GradientStop Offset="0" Color="#FF0000" /> 
      <GradientStop Offset="1" Color="#000000" /> 
     </LinearGradientBrush> 
    </Grid.Background> 

</Grid> 
</Page> 
Verwandte Themen