Exemple #1
0
        public Pen(Color_ color, float width)
        {
            Status status = GDIPlus.GdipCreatePen1(color.ToArgb(), width, GraphicsUnit.World, out nativeObject);

            GDIPlus.CheckStatus(status);
            this.color = color;
        }
Exemple #2
0
        internal static Color_ FromBGR(int bgr)
        {
            Color_ result = Color_.FromArgb(0xFF, (bgr & 0xFF), ((bgr >> 8) & 0xFF), ((bgr >> 16) & 0xFF));
            Color_ known  = KnownColors.FindColorMatch(result);

            return((known.IsEmpty) ? result : known);
        }
Exemple #3
0
        public static Color_ FromArgb(int alpha, int red, int green, int blue)
        {
            CheckARGBValues(alpha, red, green, blue);
            Color_ color = new Color_();

            color.state = (short)ColorType.ARGB;
            color.Value = (int)((uint)alpha << 24) + (red << 16) + (green << 8) + blue;
            return(color);
        }
        // methods
        public Color_ GetPixel(int x, int y)
        {
            int argb;

            Status s = GDIPlus.GdipBitmapGetPixel(nativeObject, x, y, out argb);

            GDIPlus.CheckStatus(s);

            return(Color_.FromArgb(argb));
        }
        public IntPtr GetHbitmap(Color_ background)
        {
            IntPtr HandleBmp;

            Status status = GDIPlus.GdipCreateHBITMAPFromBitmap(nativeObject, out HandleBmp, background.ToArgb());

            GDIPlus.CheckStatus(status);

            return(HandleBmp);
        }
Exemple #6
0
        /// <summary>
        ///	Equals Method
        /// </summary>
        ///
        /// <remarks>
        ///	Checks equivalence of this Color and another object.
        /// </remarks>

        public override bool Equals(object obj)
        {
            if (!(obj is Color_))
            {
                return(false);
            }
            Color_ c = (Color_)obj;

            return(this == c);
        }
Exemple #7
0
        public Pen(Brush brush, float width)
        {
            if (brush == null)
            {
                throw new ArgumentNullException("brush");
            }

            Status status = GDIPlus.GdipCreatePen2(brush.nativeObject, width, GraphicsUnit.World, out nativeObject);

            GDIPlus.CheckStatus(status);
            color = Color_.Empty;
        }
Exemple #8
0
        public static Brush FromSystemColor(Color_ c)
        {
            if (c.IsSystemColor)
            {
                SolidBrush newBrush = new SolidBrush(c);
                newBrush.isModifiable = false;
                return(newBrush);
            }

            String message = String.Format("The color {0} is not a system color.", c);

            throw new ArgumentException(message);
        }
        public static Pen FromSystemColor(Color_ c)
        {
            if (c.IsSystemColor)
            {
                Pen newPen = new Pen(c);
                newPen.isModifiable = false;
                return(newPen);
            }

            String message = String.Format("The color {0} is not a system color.", c);

            throw new ArgumentException(message);
        }
Exemple #10
0
 public static Color_ FromName(string name)
 {
     try {
         KnownColor kc = (KnownColor)Enum.Parse(typeof(KnownColor), name, true);
         return(KnownColors.FromKnownColor(kc));
     }
     catch {
         // This is what it returns!
         Color_ d = FromArgb(0, 0, 0, 0);
         d.name   = name;
         d.state |= (short)ColorType.Named;
         return(d);
     }
 }
Exemple #11
0
        public void SetPixel(int x, int y, Color_ color)
        {
            Status s = GDIPlus.GdipBitmapSetPixel(nativeObject, x, y, color.ToArgb());

            if (s == Status.InvalidParameter)
            {
                // check is done in case of an error only to avoid another
                // unmanaged call for normal (successful) calls
                if ((this.PixelFormat & PixelFormat.Indexed) != 0)
                {
                    string msg = Locale.GetText("SetPixel cannot be called on indexed bitmaps.");
                    throw new InvalidOperationException(msg);
                }
            }
            GDIPlus.CheckStatus(s);
        }
Exemple #12
0
        public void MakeTransparent(Color_ transparentColor)
        {
            // We have to draw always over a 32-bitmap surface that supports alpha channel
            Bitmap          bmp       = new Bitmap(Width, Height, PixelFormat.Format32bppArgb);
            Graphics        gr        = Graphics.FromImage(bmp);
            Rectangle       destRect  = new Rectangle(0, 0, Width, Height);
            ImageAttributes imageAttr = new ImageAttributes();

            imageAttr.SetColorKey(transparentColor, transparentColor);

            gr.DrawImage(this, destRect, 0, 0, Width, Height, GraphicsUnit.Pixel, imageAttr);

            IntPtr oldBmp = nativeObject;

            nativeObject     = bmp.nativeObject;
            bmp.nativeObject = oldBmp;

            gr.Dispose();
            bmp.Dispose();
            imageAttr.Dispose();
        }
Exemple #13
0
        public void MakeTransparent()
        {
            Color_ clr = GetPixel(0, 0);

            MakeTransparent(clr);
        }
Exemple #14
0
 public static int ToWin32(Color_ c)
 {
     // Win32Color format is BGR, Same as OleColor
     return((c.B << 16) | (c.G << 8) | c.R);
 }
Exemple #15
0
 public static int ToOle(Color_ c)
 {
     // OleColor format is BGR, same as Win32
     return((c.B << 16) | (c.G << 8) | c.R);
 }
Exemple #16
0
        public static string ToHtml(Color_ c)
        {
            if (c.IsEmpty)
            {
                return(String.Empty);
            }

            if (c.IsSystemColor)
            {
                KnownColor kc = c.ToKnownColor();
                switch (kc)
                {
                case KnownColor.ActiveBorder:
                case KnownColor.ActiveCaption:
                case KnownColor.AppWorkspace:
                case KnownColor.GrayText:
                case KnownColor.Highlight:
                case KnownColor.HighlightText:
                case KnownColor.InactiveBorder:
                case KnownColor.InactiveCaption:
                case KnownColor.InactiveCaptionText:
                case KnownColor.InfoText:
                case KnownColor.Menu:
                case KnownColor.MenuText:
                case KnownColor.ScrollBar:
                case KnownColor.Window:
                case KnownColor.WindowFrame:
                case KnownColor.WindowText:
                    return(KnownColors.GetName(kc).ToLower());

                case KnownColor.ActiveCaptionText:
                    return("captiontext");

                case KnownColor.Control:
                    return("buttonface");

                case KnownColor.ControlDark:
                    return("buttonshadow");

                case KnownColor.ControlDarkDark:
                    return("threeddarkshadow");

                case KnownColor.ControlLight:
                    return("buttonface");

                case KnownColor.ControlLightLight:
                    return("buttonhighlight");

                case KnownColor.ControlText:
                    return("buttontext");

                case KnownColor.Desktop:
                    return("background");

                case KnownColor.HotTrack:
                    return("highlight");

                case KnownColor.Info:
                    return("infobackground");

                default:
                    return(String.Empty);
                }
            }

            if (c.IsNamedColor)
            {
                if (c == Color_.LightGray)
                {
                    return("LightGrey");
                }
                else
                {
                    return(c.Name);
                }
            }

            return(FormatHtml(c.R, c.G, c.B));
        }
Exemple #17
0
        internal static Color_ StaticConvertFromString(ITypeDescriptorContext context, string s, CultureInfo culture)
        {
            if (culture == null)
            {
                culture = CultureInfo.InvariantCulture;
            }

            s = s.Trim();

            if (s.Length == 0)
            {
                return(Color_.Empty);
            }

            // Try to process both NamedColor and SystemColors from the KnownColor enumeration
            if (Char.IsLetter(s [0]))
            {
                KnownColor kc;
                try {
                    kc = (KnownColor)Enum.Parse(typeof(KnownColor), s, true);
                }
                catch (Exception e) {
                    // whatever happens MS throws an basic Exception
                    string msg = Locale.GetText("Invalid color name '{0}'.", s);
                    throw new Exception(msg, new FormatException(msg, e));
                }
                return(KnownColors.FromKnownColor(kc));
            }

            String numSeparator = culture.TextInfo.ListSeparator;
            Color_ result       = Color_.Empty;

            if (s.IndexOf(numSeparator) == -1)
            {
                bool sharp = (s[0] == '#');
                int  start = sharp ? 1 : 0;
                bool hex   = false;
                // deal with #hex, 0xhex and #0xhex
                if ((s.Length > start + 1) && (s[start] == '0'))
                {
                    hex = ((s[start + 1] == 'x') || (s[start + 1] == 'X'));
                    if (hex)
                    {
                        start += 2;
                    }
                }

                if (sharp || hex)
                {
                    s = s.Substring(start);
                    int argb;
                    try {
                        argb = Int32.Parse(s, NumberStyles.HexNumber);
                    }
                    catch (Exception e) {
                        // whatever happens MS throws an basic Exception
                        string msg = Locale.GetText("Invalid Int32 value '{0}'.", s);
                        throw new Exception(msg, e);
                    }

                    // note that the default alpha value for a 6 hex digit (i.e. when none are present) is
                    // 0xFF while shorter string defaults to 0xFF - unless both # an 0x are specified
                    if ((s.Length < 6) || ((s.Length == 6) && sharp && hex))
                    {
                        argb &= 0x00FFFFFF;
                    }
                    else if ((argb >> 24) == 0)
                    {
                        argb |= unchecked ((int)0xFF000000);
                    }
                    result = Color_.FromArgb(argb);
                }
            }

            if (result.IsEmpty)
            {
                Int32Converter converter  = new Int32Converter();
                String []      components = s.Split(numSeparator.ToCharArray());

                // MS seems to convert the indivual component to int before
                // checking the number of components
                int[] numComponents = new int[components.Length];
                for (int i = 0; i < numComponents.Length; i++)
                {
                    numComponents[i] = (int)converter.ConvertFrom(context,
                                                                  culture, components[i]);
                }

                switch (components.Length)
                {
                case 1:
                    result = Color_.FromArgb(numComponents[0]);
                    break;

                case 3:
                    result = Color_.FromArgb(numComponents[0], numComponents[1],
                                             numComponents[2]);
                    break;

                case 4:
                    result = Color_.FromArgb(numComponents[0], numComponents[1],
                                             numComponents[2], numComponents[3]);
                    break;

                default:
                    throw new ArgumentException(s + " is not a valid color value.");
                }
            }

            if (!result.IsEmpty)
            {
                // Look for a named or system color with those values
                Color_ known = KnownColors.FindColorMatch(result);
                if (!known.IsEmpty)
                {
                    return(known);
                }
            }

            return(result);
        }
Exemple #18
0
        internal Bitmap BuildBitmapOnWin32()
        {
            Bitmap bmp;

            if (imageData == null)
            {
                return(new Bitmap(32, 32));
            }

            IconImage        ii  = (IconImage)imageData [id];
            BitmapInfoHeader bih = ii.iconHeader;
            int biHeight         = bih.biHeight / 2;

            int ncolors = (int)bih.biClrUsed;

            if ((ncolors == 0) && (bih.biBitCount < 24))
            {
                ncolors = (int)(1 << bih.biBitCount);
            }

            switch (bih.biBitCount)
            {
            case 1:
                bmp = new Bitmap(bih.biWidth, biHeight, PixelFormat.Format1bppIndexed);
                break;

            case 4:
                bmp = new Bitmap(bih.biWidth, biHeight, PixelFormat.Format4bppIndexed);
                break;

            case 8:
                bmp = new Bitmap(bih.biWidth, biHeight, PixelFormat.Format8bppIndexed);
                break;

            case 24:
                bmp = new Bitmap(bih.biWidth, biHeight, PixelFormat.Format24bppRgb);
                break;

            case 32:
                bmp = new Bitmap(bih.biWidth, biHeight, PixelFormat.Format32bppArgb);
                break;

            default:
                string msg = Locale.GetText("Unexpected number of bits: {0}", bih.biBitCount);
                throw new Exception(msg);
            }

            if (bih.biBitCount < 24)
            {
                ColorPalette pal = bmp.Palette;                 // Managed palette

                for (int i = 0; i < ii.iconColors.Length; i++)
                {
                    pal.Entries[i] = Color_.FromArgb((int)ii.iconColors[i] | unchecked ((int)0xff000000));
                }
                bmp.Palette = pal;
            }

            int        bytesPerLine = (int)((((bih.biWidth * bih.biBitCount) + 31) & ~31) >> 3);
            BitmapData bits         = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.WriteOnly, bmp.PixelFormat);

            for (int y = 0; y < biHeight; y++)
            {
                Marshal.Copy(ii.iconXOR, bytesPerLine * y,
                             (IntPtr)(bits.Scan0.ToInt64() + bits.Stride * (biHeight - 1 - y)), bytesPerLine);
            }

            bmp.UnlockBits(bits);

            bmp = new Bitmap(bmp);              // This makes a 32bpp image out of an indexed one

            // Apply the mask to make properly transparent
            bytesPerLine = (int)((((bih.biWidth) + 31) & ~31) >> 3);
            for (int y = 0; y < biHeight; y++)
            {
                for (int x = 0; x < bih.biWidth / 8; x++)
                {
                    for (int bit = 7; bit >= 0; bit--)
                    {
                        if (((ii.iconAND[y * bytesPerLine + x] >> bit) & 1) != 0)
                        {
                            bmp.SetPixel(x * 8 + 7 - bit, biHeight - y - 1, Color_.Transparent);
                        }
                    }
                }
            }

            return(bmp);
        }
Exemple #19
0
 public Pen(Color_ color) : this(color, 1.0F)
 {
 }
Exemple #20
0
        private void SaveBitmapAsIcon(BinaryWriter writer)
        {
            writer.Write((ushort)0);                    // idReserved must be 0
            writer.Write((ushort)1);                    // idType must be 1
            writer.Write((ushort)1);                    // only one icon

            // when transformed into a bitmap only a single image exists
            IconDirEntry ide = new IconDirEntry();

            ide.width       = (byte)bitmap.Width;
            ide.height      = (byte)bitmap.Height;
            ide.colorCount  = 0;                // 32 bbp == 0, for palette size
            ide.reserved    = 0;                // always 0
            ide.planes      = 0;
            ide.bitCount    = 32;
            ide.imageOffset = 22;               // 22 is the first icon position (for single icon files)

            BitmapInfoHeader bih = new BitmapInfoHeader();

            bih.biSize          = (uint)Marshal.SizeOf(typeof(BitmapInfoHeader));
            bih.biWidth         = bitmap.Width;
            bih.biHeight        = 2 * bitmap.Height;      // include both XOR and AND images
            bih.biPlanes        = 1;
            bih.biBitCount      = 32;
            bih.biCompression   = 0;
            bih.biSizeImage     = 0;
            bih.biXPelsPerMeter = 0;
            bih.biYPelsPerMeter = 0;
            bih.biClrUsed       = 0;
            bih.biClrImportant  = 0;

            IconImage ii = new IconImage();

            ii.iconHeader = bih;
            ii.iconColors = new uint [0];               // no palette
            int xor_size = (((bih.biBitCount * bitmap.Width + 31) & ~31) >> 3) * bitmap.Height;

            ii.iconXOR = new byte [xor_size];
            int p = 0;

            for (int y = bitmap.Height - 1; y >= 0; y--)
            {
                for (int x = 0; x < bitmap.Width; x++)
                {
                    Color_ c = bitmap.GetPixel(x, y);
                    ii.iconXOR [p++] = c.B;
                    ii.iconXOR [p++] = c.G;
                    ii.iconXOR [p++] = c.R;
                    ii.iconXOR [p++] = c.A;
                }
            }
            int and_line_size = (((Width + 31) & ~31) >> 3);                    // must be a multiple of 4 bytes
            int and_size      = and_line_size * bitmap.Height;

            ii.iconAND = new byte [and_size];

            ide.bytesInRes = (uint)(bih.biSize + xor_size + and_size);

            SaveIconDirEntry(writer, ide, UInt32.MaxValue);
            SaveIconImage(writer, ii);
        }
Exemple #21
0
 public static Color_ FromArgb(int alpha, Color_ baseColor)
 {
     return(FromArgb(alpha, baseColor.R, baseColor.G, baseColor.B));
 }
Exemple #22
0
        public override object ConvertTo(ITypeDescriptorContext context,
                                         CultureInfo culture,
                                         object value,
                                         Type destinationType)
        {
            if (value is Color_)
            {
                Color_ color = (Color_)value;
                if (destinationType == typeof(string))
                {
                    if (color == Color_.Empty)
                    {
                        return(string.Empty);
                    }

                    if (color.IsKnownColor || color.IsNamedColor)
                    {
                        return(color.Name);
                    }

                    String numSeparator = culture.TextInfo.ListSeparator;

                    StringBuilder sb = new StringBuilder();
                    if (color.A != 255)
                    {
                        sb.Append(color.A);
                        sb.Append(numSeparator);
                        sb.Append(" ");
                    }
                    sb.Append(color.R);
                    sb.Append(numSeparator);
                    sb.Append(" ");

                    sb.Append(color.G);
                    sb.Append(numSeparator);
                    sb.Append(" ");

                    sb.Append(color.B);
                    return(sb.ToString());
                }
                else if (destinationType == typeof(InstanceDescriptor))
                {
                    if (color.IsEmpty)
                    {
                        return(new InstanceDescriptor(typeof(Color_).GetField("Empty"), null));
                    }
                    else if (color.IsSystemColor)
                    {
                        return(new InstanceDescriptor(typeof(SystemColors).GetProperty(color.Name), null));
                    }
                    else if (color.IsKnownColor)
                    {
                        return(new InstanceDescriptor(typeof(Color_).GetProperty(color.Name), null));
                    }
                    else
                    {
                        MethodInfo met = typeof(Color_).GetMethod("FromArgb", new Type[] { typeof(int), typeof(int), typeof(int), typeof(int) });
                        return(new InstanceDescriptor(met, new object[] { color.A, color.R, color.G, color.B }));
                    }
                }
            }

            return(base.ConvertTo(context, culture, value, destinationType));
        }