代码之家  ›  专栏  ›  技术社区  ›  devuxer

.NET Framework 3.5是否有hsbtorgb转换器,或者是否需要自己滚动?

  •  12
  • devuxer  · 技术社区  · 14 年前

    我在文档中搜索了一个hsbtorgb转换器,但没有找到任何包含“hsb”或“hsl”的内容,所以我猜想它根本不存在。不过,为了确保有支持这种转换的类吗?

    更新

    我最终选择了这个,这与0xa3略有不同。我还添加了一个 AhsbToArgb 所以我可以转换成RGB,一次设置alpha通道。

    阿什布托尔布 -允许阿尔法通道:

    public static Color AhsbToArgb(byte a, double h, double s, double b)
    {
        var color = HsbToRgb(h, s, b);
        return Color.FromArgb(a, color.R, color.G, color.B);
    }
    

    HsbToRgb -将色调饱和度亮度转换为红-绿-蓝:

    public static Color HsbToRgb(double h, double s, double b)
    {
        if (s == 0)
            return RawRgbToRgb(b, b, b);
        else
        {
            var sector = h / 60;
            var sectorNumber = (int)Math.Truncate(sector);
            var sectorFraction = sector - sectorNumber;
            var b1 = b * (1 - s);
            var b2 = b * (1 - s * sectorFraction);
            var b3 = b * (1 - s * (1 - sectorFraction));
            switch (sectorNumber)
            {
                case 0:
                    return RawRgbToRgb(b, b3, b1);
                case 1:
                    return RawRgbToRgb(b2, b, b1);
                case 2:
                    return RawRgbToRgb(b1, b, b3);
                case 3:
                    return RawRgbToRgb(b1, b2, b);
                case 4:
                    return RawRgbToRgb(b3, b1, b);
                case 5:
                    return RawRgbToRgb(b, b1, b2);
                default:
                    throw new ArgumentException("Hue must be between 0 and 360");
            }
        }
    }
    

    RawRgbToRgb -将double转换为int并返回color对象:

    private static Color RawRgbToRgb(double rawR, double rawG, double rawB)
    {
        return Color.FromArgb(
            (int)Math.Round(rawR * 255),
            (int)Math.Round(rawG * 255),
            (int)Math.Round(rawB * 255));
    }
    
    2 回复  |  直到 14 年前
        1
  •  14
  •   MusiGenesis    14 年前

    不,据我所知。但是算法并不是很复杂,您将在Web上找到工作代码,比如本codeproject文章中的代码。 Manipulating colors in .NET 作者:Guillaume Lepamentier:

    public static Color HSBtoRGB(double hue, double saturation, double brightness)
    {
        double r = 0;
        double g = 0;
        double b = 0;
    
        if (saturation == 0)
        {
            r = g = b = brightness;
        }
        else
        {
            // The color wheel consists of 6 sectors. 
            // Figure out which sector you're in.
            //
            double sectorPos = hue / 60.0;
            int sectorNumber = (int)(Math.Floor(sectorPos));
    
            // get the fractional part of the sector
            double fractionalSector = sectorPos - sectorNumber;
    
            // calculate values for the three axes of the color. 
            double p = brightness * (1.0 - saturation);
            double q = brightness * (1.0 - (saturation * fractionalSector));
            double t = brightness * (1.0 - (saturation * (1 - fractionalSector)));
    
            // assign the fractional colors to r, g, and b 
            // based on the sector the angle is in.
            switch (sectorNumber)
            {
                case 0:
                    r = brightness;
                    g = t;
                    b = p;
                    break;
                case 1:
                    r = q;
                    g = brightness;
                    b = p;
                    break;
                case 2:
                    r = p;
                    g = brightness;
                    b = t;
                    break;
                case 3:
                    r = p;
                    g = q;
                    b = brightness;
                    break;
                case 4:
                    r = t;
                    g = p;
                    b = brightness;
                    break;
                case 5:
                    r = brightness;
                    g = p;
                    b = q;
                    break;
            }
        }
    
        return Color.FromArgb(
            (int)(r * 255.0 + 0.5), 
            (int)(g * 255.0 + 0.5), 
            (int)(b * 255.0 + 0.5));
    }
    
        2
  •  5
  •   MusiGenesis    14 年前

    不,在.NET中(最多包括4.0) Color 类仅自动从rgb转换为hsb(通过 GetHue , GetSaturation GetBrightness 方法)。您必须滚动自己的方法,从hsb值转到rgb值,或者使用已经编写的示例,例如:

    http://www.codeproject.com/KB/GDI-plus/HSBColorClass.aspx