zoukankan      html  css  js  c++  java
  • C# RGB和HSB相互转换

    背景 

        最近做的项目中有这样一个场景,设置任意一种颜色,得到这种颜色偏深和偏浅的两种颜色。也就是说取该颜色同色系的深浅两种颜色。首先想到的是调节透明度,但效果不理想。后来尝试调节颜色亮度,发现这才是正解。但是.NET中不能直接改变Color的亮度,需要将Color转换成HSB模式,然后改变B的值调节亮度。调节亮度后需要再转换成我们熟悉的RGB模式才能使用。下面给出颜色转换方法。

    代码 

     1     /// <summary>
     2     /// 颜色转换帮助类
     3     /// </summary>
     4     public class ColorConversionHelper
     5     {
     6         /// <summary>
     7         ///  RGB转HSB
     8         /// </summary>
     9         /// <param name="red">红色值</param>
    10         /// <param name="green">绿色值</param>
    11         /// <param name="blue">蓝色值</param>
    12         /// <returns>返回:HSB值集合</returns>
    13         public static List<float> RGBtoHSB(int red, int green, int blue)
    14         {
    15             List<float> hsbList = new List<float>();
    16             System.Drawing.Color dColor = System.Drawing.Color.FromArgb(red, green, blue);
    17             hsbList.Add(dColor.GetHue());
    18             hsbList.Add(dColor.GetSaturation());
    19             hsbList.Add(dColor.GetBrightness());
    20             return hsbList;
    21         }
    22 
    23 
    24         /// <summary>
    25         /// HSB转RGB
    26         /// </summary>
    27         /// <param name="hue">色调</param>
    28         /// <param name="saturation">饱和度</param>
    29         /// <param name="brightness">亮度</param>
    30         /// <returns>返回:Color</returns>
    31         public static Color HSBtoRGB(float hue, float saturation, float brightness)
    32         {
    33             int r = 0, g = 0, b = 0;
    34             if (saturation == 0)
    35             {
    36                 r = g = b = (int)(brightness * 255.0f + 0.5f);
    37             }
    38             else
    39             {
    40                 float h = (hue - (float)Math.Floor(hue)) * 6.0f;
    41                 float f = h - (float)Math.Floor(h);
    42                 float p = brightness * (1.0f - saturation);
    43                 float q = brightness * (1.0f - saturation * f);
    44                 float t = brightness * (1.0f - (saturation * (1.0f - f)));
    45                 switch ((int)h)
    46                 {
    47                     case 0:
    48                         r = (int)(brightness * 255.0f + 0.5f);
    49                         g = (int)(t * 255.0f + 0.5f);
    50                         b = (int)(p * 255.0f + 0.5f);
    51                         break;
    52                     case 1:
    53                         r = (int)(q * 255.0f + 0.5f);
    54                         g = (int)(brightness * 255.0f + 0.5f);
    55                         b = (int)(p * 255.0f + 0.5f);
    56                         break;
    57                     case 2:
    58                         r = (int)(p * 255.0f + 0.5f);
    59                         g = (int)(brightness * 255.0f + 0.5f);
    60                         b = (int)(t * 255.0f + 0.5f);
    61                         break;
    62                     case 3:
    63                         r = (int)(p * 255.0f + 0.5f);
    64                         g = (int)(q * 255.0f + 0.5f);
    65                         b = (int)(brightness * 255.0f + 0.5f);
    66                         break;
    67                     case 4:
    68                         r = (int)(t * 255.0f + 0.5f);
    69                         g = (int)(p * 255.0f + 0.5f);
    70                         b = (int)(brightness * 255.0f + 0.5f);
    71                         break;
    72                     case 5:
    73                         r = (int)(brightness * 255.0f + 0.5f);
    74                         g = (int)(p * 255.0f + 0.5f);
    75                         b = (int)(q * 255.0f + 0.5f);
    76                         break;
    77                 }
    78             }
    79             return Color.FromArgb(Convert.ToByte(255), Convert.ToByte(r), Convert.ToByte(g), Convert.ToByte(b));
    80         }
    81     }

     

    扩展阅读

    http://stackoverflow.com/questions/4106363/converting-rgb-to-hsb-colors

    http://my.oschina.net/soitravel/blog/79862

     

  • 相关阅读:
    opencv安装
    安装电脑
    勿忘心安
    Linux操作
    listBox1_DrawItem
    今天被骂
    我研究群体行为,希望大家一起讨论
    Matlab高级绘图
    网址
    下面哪些机制可以用于进程间通信?
  • 原文地址:https://www.cnblogs.com/leolion/p/4732935.html
Copyright © 2011-2022 走看看