如何将一系列颜色映射到一系列整数?
具体来说,我希望滑块从深绿色变为黄色再变为红色。
以下代码仅为颜色添加阴影。
XAML:
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var args = parameter as string;
var minimumInput = int.Parse(args?.Split('~')?[0]);
var maximumInput = int.Parse(args?.Split('~')?[1]);
var currentValue = (double)value;
var colorValue = (int)255 - (currentValue * (COLOR_RANGE_MAX / maximumInput));
var color = Color.FromArgb(COLOR_RANGE_MAX, 0, System.Convert.ToByte(colorValue), 0);
return new SolidColorBrush(color);
}
ValueConverter:
namespace MotoLens
{
class ValueToBrushConverter : IValueConverter
{
static readonly Color[] _colorTable =
{
Color.FromRgb( 0, 255, 255),
Color.FromRgb( 0, 255, 0),
Color.FromRgb(255, 255, 0),
Color.FromRgb(255, 0, 0),
};
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var args = parameter as string;
var minimumInput = int.Parse(args.Split('~')[0]);
var maximumInput = int.Parse(args.Split('~')[1]);
var currentValue = ((double)value - minimumInput) / (maximumInput - minimumInput);
var col1 = (int)(currentValue * (_colorTable.Length - 1));
var col2 = Math.Min(col1 + 1, (_colorTable.Length - 1));
var t = 1.0 / (_colorTable.Length - 1);
return new SolidColorBrush(Lerp(_colorTable[col1], _colorTable[col2], (currentValue - t * col1) / t));
}
public static Color Lerp(Color col1, Color col2, double t)
{
var r = col1.R * (1 - t) + col2.R * t;
var g = col1.G * (1 - t) + col2.G * t;
var b = col1.B * (1 - t) + col2.B * t;
return Color.FromRgb((byte)r, (byte)g, (byte)b);
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
}
注意: 我的记忆告诉我,我应该让框架通过动画为我做这件事。 不确定它是彩色动画还是双动画。
通过答案更新:
public class Category
{
public int Category1 {get;set;}
public int Category2 {get;set;}
public int Category3 {get;set;}
}
答案 0 :(得分:1)
试试这个:


 private static readonly Color [] ColorTable =
 {
 Color.FromRgb(255,0,0),
 Color.FromRgb(255,255,0),
 Color.FromRgb(0,255,0),
 Color.FromRgb(0,255,255),
 Color.FromRgb(0,0,255),
 Color.FromRgb(255,0,255),
 Color.FromRgb(255,0,0),
 };

 public object Convert(object value,Type targetType,object parameter,CultureInfo culture)
 {
 var args =参数为字符串;
 var minimumInput = int.Parse(args.Split('〜')[0]);
 var maximumInput = int.Parse(args.Split('〜')[1]);
 var currentValue =((double)value - minimumInput)/(maximumInput - minimumInput);
 var col1 =(int)(currentValue *(ColorTable.Length-1));
 var col2 = Math.Min(col1 + 1,(ColorTable.Length-1));
 var t = 1.0 /(ColorTable.Length - 1);
返回新的SolidColorBrush(Lerp(ColorTable [col1],ColorTable [col2],(currentValue - t * col1)/ t));
 }

 public static Color Lerp(Color col1,Color col2,double t)
 {
 var r = col1.R *(1-t)+ col2.R * t;
 var g = col1.G *(1-t)+ col2.G * t;
 var b = col1.B *(1-t)+ col2.B * t;
返回Color.FromRgb((byte)r,(byte)g,(byte)b);
 }
 代码>


答案 1 :(得分:0)
你可以根据我的ColorExtensions
做一些有HSL或HSV转换为RGB的方法。例如
#region FromHsv()
/// <summary>
/// Returns a Color struct based on HSV model.
/// </summary>
/// <param name="hue">0..360 range hue</param>
/// <param name="saturation">0..1 range saturation</param>
/// <param name="value">0..1 range value</param>
/// <param name="alpha">0..1 alpha</param>
/// <returns></returns>
public static Color FromHsv(double hue, double saturation, double value, double alpha = 1.0)
{
Debug.Assert(hue >= 0);
Debug.Assert(hue <= 360);
double chroma = value * saturation;
double h1 = hue / 60;
double x = chroma * (1 - Math.Abs(h1 % 2 - 1));
double m = value - chroma;
double r1, g1, b1;
if (h1 < 1)
{
r1 = chroma;
g1 = x;
b1 = 0;
}
else if (h1 < 2)
{
r1 = x;
g1 = chroma;
b1 = 0;
}
else if (h1 < 3)
{
r1 = 0;
g1 = chroma;
b1 = x;
}
else if (h1 < 4)
{
r1 = 0;
g1 = x;
b1 = chroma;
}
else if (h1 < 5)
{
r1 = x;
g1 = 0;
b1 = chroma;
}
else //if (h1 < 6)
{
r1 = chroma;
g1 = 0;
b1 = x;
}
byte r = (byte)(255 * (r1 + m));
byte g = (byte)(255 * (g1 + m));
byte b = (byte)(255 * (b1 + m));
byte a = (byte)(255 * alpha);
return Color.FromArgb(a, r, g, b);
}
#endregion
示例 - XAML:
<StackPanel>
<Slider
x:Name="HueSlider"
Minimum="0"
Maximum="360"
StepFrequency="1"
ValueChanged="HueSlider_ValueChanged" />
<Rectangle
x:Name="HueRect"
Width="400"
Height="200" />
</StackPanel>
代码背后:
private void HueSlider_ValueChanged(object sender, RangeBaseValueChangedEventArgs e)
{
HueRect.Fill = new SolidColorBrush(
FromHsv(e.NewValue, 1, 1));
}