zoukankan      html  css  js  c++  java
  • 根据温度获取颜色

    最近需要做一个功能,就是根据温度显示物体的颜色,类似中国天气网:

     

     /// <summary>

        /// 根据温度获取颜色
        
    /// 算法如下:
        
    /// 1.定义色阶变化范围fromColor, toColor
        
    /// 2.获取因子:factor = T / (MaxT - MinT);
        
    /// 3.计算新的RGB
        
    ///   R = fromColor.R * (1 - factor) + toColor.R * factor
        
    ///   G = fromColor.G * (1 - factor) + toColor.G * factor
        
    ///   B = fromColor.B * (1 - factor) * toColor.B * factor
        
    /// 4.如果多个色阶,重复上面的运算
        
    /// </summary>
        public static class TemperatuerColor
        {
            private static Color Red = Color.FromRgb(25500);
            private static Color Yellow = Color.FromRgb(25500);
            private static Color Blue = Color.FromRgb(25500);

            
            public static SolidColorBrush GetColor(double temperature, double maxtemperature, double mintemperature)
            {
                double ratio = temperature / (maxtemperature - mintemperature);
                Color result1 = Compute(Blue, Yellow, ratio);
                Color result2 = Compute(Yellow, Red, ratio);
                Color result =  Compute(result1, result2, ratio);

                return new SolidColorBrush(result);
            }

            private static Color Compute(Color fromColor, Color toColor, double ratio)
            {
                byte r = (byte)(fromColor.R * (1.0f - ratio) + toColor.R * ratio);
                byte g = (byte)(fromColor.G * (1.0f - ratio) + toColor.G * ratio);
                byte b = (byte)(fromColor.B * (1.0f - ratio) + toColor.B * ratio);
                return Color.FromRgb(r, g, b);
            }

        }

     我这里的颜色是红色->黄色->蓝色的渐变。如果需要增加新的色阶,就需要增加新的颜色.

    Color result1 = Compute(Blue, Yellow, ratio);            

    Color result2 = Compute(Yellow, Red, ratio);
    Color result =  Compute(result1, result2, ratio);

  • 相关阅读:
    程序员之道——编程也是一门艺术
    Spring动态获取IoC容器中管理的Bean
    SVN使用教程之——分支、合并
    WebSphere从Windows迁移至Linux出现org.dom4j.DocumentException异常:Nested exception: prolog 中不允许有内容
    Spring动态获取IoC容器中管理的Bean
    com.microsoft.sqlserver.jdbc.SQLServerException: 将截断字符串或二进制数据
    IMP导入数据 出现ORA01691问题 解决办法
    jquery对象与Dom对象的相互转化
    json<>js
    photoshop快捷键
  • 原文地址:https://www.cnblogs.com/enjoyeclipse/p/2320487.html
Copyright © 2011-2022 走看看