Example #1
0
        private Bitmap GetColorPlaneBitmap(Rectangle rect, ColorComponent comp)
        {
            Bitmap     map     = new Bitmap(rect.Width, rect.Height, PixelFormat.Format24bppRgb);
            BitmapData mapData = map.LockBits(
                new Rectangle(0, 0, map.Width, map.Height),
                ImageLockMode.WriteOnly,
                PixelFormat.Format24bppRgb);

            unsafe
            {
                byte *pt0 = (byte *)mapData.Scan0;

                Parallel.For(rect.Top, rect.Bottom, y =>
                {
                    int bitmapY = y - rect.Top;

                    for (int x = rect.Left; x < rect.Right; x++)
                    {
                        int bitmapX = x - rect.Left;

                        Color color;
                        switch (comp)
                        {
                        case ColorComponent.Hue:
                            color = AdobeColors.HSB_to_RGB(new AdobeColors.HSB(
                                                               _hsb.H,
                                                               x / 255.0,
                                                               1 - y / 255.0));
                            break;

                        case ColorComponent.Saturation:
                            color = AdobeColors.HSB_to_RGB(new AdobeColors.HSB(
                                                               x / 255.0,
                                                               _hsb.S,
                                                               1 - y / 255.0));
                            break;

                        case ColorComponent.Brightness:
                            color = AdobeColors.HSB_to_RGB(new AdobeColors.HSB(
                                                               x / 255.0,
                                                               1 - y / 255.0,
                                                               _hsb.B));
                            break;

                        case ColorComponent.Red:
                            color = Color.FromArgb(
                                _rgb.R,
                                x,
                                255 - y);
                            break;

                        case ColorComponent.Green:
                            color = Color.FromArgb(
                                x,
                                _rgb.G,
                                255 - y);
                            break;

                        case ColorComponent.Blue:
                            color = Color.FromArgb(
                                x,
                                255 - y,
                                _rgb.B);
                            break;

                        default:
                            throw new ArgumentException();
                        }

                        if (_webSafeColorsOnly)
                        {
                            color = AdobeColors.GetNearestWebSafeColor(color);
                        }

                        byte *pt = pt0 + mapData.Stride * bitmapY + 3 * bitmapX;
                        pt[2]    = color.R;
                        pt[1]    = color.G;
                        pt[0]    = color.B;
                    }
                });
            }

            map.UnlockBits(mapData);
            return(map);
        }
        private Bitmap GetColorStripBitmap(Rectangle rect, ColorComponent comp)
        {
            Bitmap     map     = new Bitmap(rect.Width, rect.Height, PixelFormat.Format24bppRgb);
            BitmapData mapData = map.LockBits(
                new Rectangle(0, 0, map.Width, map.Height),
                ImageLockMode.WriteOnly,
                PixelFormat.Format24bppRgb);

            int height = this.Height - 8;

            unsafe
            {
                byte *pt0 = (byte *)mapData.Scan0;

                Parallel.For(rect.Top, rect.Bottom, y =>
                {
                    int bitmapY = y - rect.Top;

                    Color color;
                    switch (comp)
                    {
                    case ColorComponent.Hue:
                        color = AdobeColors.HSB_to_RGB(new AdobeColors.HSB(
                                                           1.0 - (double)y / height,
                                                           1,
                                                           1));
                        break;

                    case ColorComponent.Saturation:
                        color = AdobeColors.HSB_to_RGB(new AdobeColors.HSB(
                                                           _hsb.H,
                                                           1.0 - (double)y / height,
                                                           _hsb.B));
                        break;

                    case ColorComponent.Brightness:
                        color = AdobeColors.HSB_to_RGB(new AdobeColors.HSB(
                                                           _hsb.H,
                                                           _hsb.S,
                                                           1.0 - (double)y / height));
                        break;

                    case ColorComponent.Red:
                        int red = 255 - (int)Math.Round(255 * (double)y / height);
                        color   = Color.FromArgb(
                            red,
                            _rgb.G,
                            _rgb.B);
                        break;

                    case ColorComponent.Green:
                        int green = 255 - (int)Math.Round(255 * (double)y / height);
                        color     = Color.FromArgb(
                            _rgb.R,
                            green,
                            _rgb.B);
                        break;

                    case ColorComponent.Blue:
                        int blue = 255 - (int)Math.Round(255 * (double)y / height);
                        color    = Color.FromArgb(
                            _rgb.R,
                            _rgb.G,
                            blue);
                        break;

                    default:
                        throw new ArgumentException();
                    }

                    if (_webSafeColorsOnly)
                    {
                        color = AdobeColors.GetNearestWebSafeColor(color);
                    }

                    for (int x = rect.Left; x < rect.Right; x++)
                    {
                        int bitmapX = x - rect.Left;

                        byte *pt = pt0 + mapData.Stride * bitmapY + 3 * bitmapX;
                        pt[2]    = color.R;
                        pt[1]    = color.G;
                        pt[0]    = color.B;
                    }
                });
            }

            map.UnlockBits(mapData);
            return(map);
        }