如何从XAML LinearGradientBrush的偏移量读取颜色? [英] How to read the color from an offset of a XAML LinearGradientBrush?
本文介绍了如何从XAML LinearGradientBrush的偏移量读取颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
给出如下定义的LinearGradientBrush
:
<LinearGradientBrush x:Key="RedYellowGradient">
<GradientStop Color="Blue" Offset="0.01" />
<GradientStop Color="Purple" Offset="0.25"/>
<GradientStop Color="Red" Offset="0.5"/>
<GradientStop Color="Orange" Offset="0.75"/>
<GradientStop Color="Yellow" Offset="1.0"/>
</LinearGradientBrush>
要采用该定义并确定由特定偏移量(例如0.13或0.82)表示的颜色而不呈现任何可见内容,需要做什么?
What is required to take that definition and determine the color represented by a specific offset, such as 0.13 or 0.82 without rendering anything visible?
这将采用带有原型的函数形式:
This would take the form of a function with a prototype something like this:
Function GetColorFromBrushOffset(br as LinearGradientBrush, offset as Single) as SomeColorDataStructure
在功能主体中需要输入什么?我不是在寻找完成的代码(尽管我不会拒绝它!)只是一些关于要使用什么数据结构和系统调用的想法.
What would need to go in the function body? I'm not looking for finished code (though I won't refuse it!) just some ideas about what data structures and system calls to use.
推荐答案
This class (from one of this question's answers by @JonnyPiazzi) appears to exactly address my question:
public static class GradientStopCollectionExtensions
{
public static Color GetRelativeColor(this GradientStopCollection gsc, double offset)
{
GradientStop before = gsc.Where(w => w.Offset == gsc.Min(m => m.Offset)).First();
GradientStop after = gsc.Where(w => w.Offset == gsc.Max(m => m.Offset)).First();
foreach (var gs in gsc)
{
if (gs.Offset < offset && gs.Offset > before.Offset)
{
before = gs;
}
if (gs.Offset > offset && gs.Offset < after.Offset)
{
after = gs;
}
}
var color = new Color();
color.ScA = (float)((offset - before.Offset) * (after.Color.ScA - before.Color.ScA) / (after.Offset - before.Offset) + before.Color.ScA);
color.ScR = (float)((offset - before.Offset) * (after.Color.ScR - before.Color.ScR) / (after.Offset - before.Offset) + before.Color.ScR);
color.ScG = (float)((offset - before.Offset) * (after.Color.ScG - before.Color.ScG) / (after.Offset - before.Offset) + before.Color.ScG);
color.ScB = (float)((offset - before.Offset) * (after.Color.ScB - before.Color.ScB) / (after.Offset - before.Offset) + before.Color.ScB);
return color;
}
}
这篇关于如何从XAML LinearGradientBrush的偏移量读取颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文