获得颜色梯度具体位置 [英] Get color in specific location on gradient

查看:177
本文介绍了获得颜色梯度具体位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有GradientStopCollection:

I have GradientStopCollection:

GradientStopCollection grsc = new GradientStopCollection(3);
grsc.Add(new GradientStop(Colors.Red, 0));
grsc.Add(new GradientStop(Colors.Yellow, .5));
grsc.Add(new GradientStop(Colors.Green, 1));

我能在特定位置的颜色? 例如: 位置0:红 地点0.5:黄色 位置0.75:??

Can I get the color in specific location? For example: Location 0: Red Location .5: Yellow Location .75: ??

还有就是可以这样做第三方类?

There is Third party class that can do so?

推荐答案

要获得一种颜色在一个特定的点有必要了解有关的梯度,这不是阶级GradientStopCollection的作用。这个类的概念是不明白的梯度,而应该是支持的简单集合的渐变。

To get a color at a specific point is necessary to understand the gradient in question, and this is not the role of class GradientStopCollection. The concept of this class is not to understand a gradient, but should be a simple collection of support to a gradient.

重要的是,你了解每一个类的概念。

Is important that you understand the concept of each class.

要得到一个颜色,你需要实例化再利用梯度作画,最后得到这幅画的颜色presents一个渐变的类。

To get a color, you need to instantiate a class that represents a gradient using the gradient to paint and finally get their color from the painting.

但我会给你一个更快的解决方案。您可以使用渐变算法来生成一个点。这是如何使用的线性梯度算法来做到这一点的实现:

but I'll give you a quicker solution. You can use a gradient algorithm to generate a single point. This is an implementation of how to do this using a linear gradient algorithm:

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;
    }
}

添加这个类在当前的情况下(名称空间上下文)

Add this class in your current context (namespace context)

要得到你的颜色在你插入像这样的任何地方:

To get your color in any place you insert something like this:

var color = grsc.GetRelativeColor(.75);

这篇关于获得颜色梯度具体位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆