1

我正在尝试将 g1 的颜色设置为 HSBColor,我正在将 Java 程序转换为在 c# 中运行,所以需要有人指出我正确的方向。

 g1 = Graphics.FromImage(picture);
 g1 = Color.????

下面是为 HSBColor 创建的结构:

public struct HSBColor
                {
                    float h;
                    float s;
                    float b;
                    int a;

                    public HSBColor(float h, float s, float b)
                    {
                        this.a = 0xff;
                        this.h = Math.Min(Math.Max(h, 0), 255);
                        this.s = Math.Min(Math.Max(s, 0), 255);
                        this.b = Math.Min(Math.Max(b, 0), 255);
                    }

                    public HSBColor(int a, float h, float s, float b)
                    {
                        this.a = a;
                        this.h = Math.Min(Math.Max(h, 0), 255);
                        this.s = Math.Min(Math.Max(s, 0), 255);
                        this.b = Math.Min(Math.Max(b, 0), 255);
                    }

                    public float H
                    {
                        get { return h; }
                    }

                    public float S
                    {
                        get { return s; }
                    }

                    public float B
                    {
                        get { return b; }
                    }

                    public int A
                    {
                        get { return a; }
                    }

                    public Color Color
                    {
                        get
                        {
                            return FromHSB(this);
                        }
                    }

                    public static Color FromHSB(HSBColor hsbColor)
                    {
                        float r = hsbColor.b;
                        float g = hsbColor.b;
                        float b = hsbColor.b;
                        if (hsbColor.s != 0)
                        {
                            float max = hsbColor.b;
                            float dif = hsbColor.b * hsbColor.s / 255f;
                            float min = hsbColor.b - dif;

                            float h = hsbColor.h * 360f / 255f;

                            if (h < 60f)
                            {
                                r = max;
                                g = h * dif / 60f + min;
                                b = min;
                            }
                            else if (h < 120f)
                            {
                                r = -(h - 120f) * dif / 60f + min;
                                g = max;
                                b = min;
                            }
                            else if (h < 180f)
                            {
                                r = min;
                                g = max;
                                b = (h - 120f) * dif / 60f + min;
                            }
                            else if (h < 240f)
                            {
                                r = min;
                                g = -(h - 240f) * dif / 60f + min;
                                b = max;
                            }
                            else if (h < 300f)
                            {
                                r = (h - 240f) * dif / 60f + min;
                                g = min;
                                b = max;
                            }
                            else if (h <= 360f)
                            {
                                r = max;
                                g = min;
                                b = -(h - 360f) * dif / 60 + min;
                            }
                            else
                            {
                                r = 0;
                                g = 0;
                                b = 0;
                            }
                        }

                        return Color.FromArgb
                            (
                                hsbColor.a,
                                (int)Math.Round(Math.Min(Math.Max(r, 0), 255)),
                                (int)Math.Round(Math.Min(Math.Max(g, 0), 255)),
                                (int)Math.Round(Math.Min(Math.Max(b, 0), 255))
                                );
                    }

            }

谁能指出我将g1设置为hsbcolor的正确方向?

4

0 回答 0