Example #1
0
        /// <summary>画像の明るさを得る</summary>
        /// <param name="bmp">画像</param>
        /// <returns>光度ラベル</returns>
        public LabelStructure GetBrightness(Bitmap bmp)
        {
            LabelStructure label = new LabelStructure(bmp.Width, bmp.Height);

            BitmapData data = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);

            byte[] buf = new byte[bmp.Width * bmp.Height * 4];
            Marshal.Copy(data.Scan0, buf, 0, buf.Length);
            bmp.UnlockBits(data);

            for (int i = 0; i < buf.Length; i += 4)
            {
                HSV hsv = HSV.FromRGB(buf[i + 0], buf[i + 1], buf[i + 2]);
                int x   = i / 4 % bmp.Width;
                int y   = i / 4 / bmp.Width;

                label[y, x] = (int)(hsv.V * 255);
            }

            return(label);
        }
Example #2
0
        /// <summary>Colorクラスを作成する</summary>
        /// <param name="c">色</param>
        /// <returns>RGBの色<see cref="Color"/></returns>
        public static Color ToRGB(HSV c)
        {
            double v = c.V;
            double s = c.S;
            double r, g, b;

            if (s == 0)
            {
                r = g = b = v;
            }
            else
            {
                double h = c.H / 60;
                int    i = (int)Math.Floor(h);
                double f = h - i;
                double p = v * (1 - s);
                double q;
                if (i % 2 == 0)
                {
                    q = v * (1 - (1 - f) * s);
                }
                else
                {
                    q = v * (1 - f * s);
                }

                switch (i)
                {
                case 0:
                    r = v;
                    g = q;
                    b = p;
                    break;

                case 1:
                    r = q;
                    g = v;
                    b = p;
                    break;

                case 2:
                    r = p;
                    g = v;
                    b = q;
                    break;

                case 3:
                    r = p;
                    g = q;
                    b = v;
                    break;

                case 4:
                    r = q;
                    g = p;
                    b = v;
                    break;

                case 5:
                    r = v;
                    g = p;
                    b = q;
                    break;

                default:
                    throw new ArgumentException("色相の値が不正です", "HSV");
                }
            }
            return(Color.FromArgb((int)Math.Round(r * 255f), (int)Math.Round(g * 255f), (int)Math.Round(b * 255f)));
        }