**** Merged from MCS ****
[mono-project.git] / mcs / class / System.Drawing / System.Drawing.Imaging / ColorPalette.cs
blob94fc458eebe8753a5b3294039026ad77f1bcedcc
1 //
2 // System.Drawing.Imaging.ColorPalette.cs
3 //
4 // (C) 2002 Ximian, Inc. http://www.ximian.com
5 //
6 // Author:
7 // Miguel de Icaza (miguel@ximian.com)
8 //
11 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
20 //
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
23 //
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 using System;
34 using System.Drawing;
35 using System.Runtime.InteropServices;
37 namespace System.Drawing.Imaging
39 public sealed class ColorPalette {
40 // 0x1: the color values in the array contain alpha information
41 // 0x2: the color values are grayscale values.
42 // 0x4: the colors in the array are halftone values.
44 private int flags;
45 private Color [] entries;
48 // There is no public constructor, this will be used somewhere in the
49 // drawing code
51 internal ColorPalette ()
53 flags = 0;
54 entries = new Color [0];
57 internal ColorPalette (int flags, Color[] colors) {
58 this.flags = flags;
59 entries = colors;
62 public Color [] Entries {
63 get {
64 return entries;
68 public int Flags {
69 get {
70 return flags;
74 /* Caller should call FreeHGlobal*/
75 internal IntPtr getGDIPalette()
77 GdiColorPalette palette = new GdiColorPalette ();
78 Color[] entries = Entries;
79 int entry = 0;
80 int size = Marshal.SizeOf (palette) + (Marshal.SizeOf (entry) * entries.Length);
81 IntPtr lfBuffer = Marshal.AllocHGlobal(size);
83 palette.Flags = Flags;
84 palette.Count = entries.Length;
86 int[] values = new int[palette.Count];
88 for (int i = 0; i < values.Length; i++) {
89 values[i] = entries[i].ToArgb();
90 //Console.Write("{0:X} ;", values[i]);
93 //Console.WriteLine("pal size " + Marshal.SizeOf (palette) + " native " + NativeObject);
95 Marshal.StructureToPtr (palette, lfBuffer, false);
96 Marshal.Copy (values, 0, (IntPtr) (lfBuffer.ToInt32() + Marshal.SizeOf (palette)), values.Length);
98 return lfBuffer;
101 internal ColorPalette Clone ()
103 ColorPalette clone = new ColorPalette ();
105 clone.flags = flags;
106 clone.entries = new Color [entries.Length];
107 for (int i = 0; i < entries.Length; i++)
108 clone.entries [i] = entries [i];
110 return clone;
113 internal void setFromGDIPalette(IntPtr palette)