ColorPalette.cs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. //
  2. // System.Drawing.Imaging.ColorPalette.cs
  3. //
  4. // (C) 2002 Ximian, Inc. http://www.ximian.com
  5. //
  6. // Author:
  7. // Miguel de Icaza ([email protected])
  8. //
  9. using System;
  10. using System.Drawing;
  11. using System.Runtime.InteropServices;
  12. namespace System.Drawing.Imaging
  13. {
  14. public sealed class ColorPalette {
  15. // 0x1: the color values in the array contain alpha information
  16. // 0x2: the color values are grayscale values.
  17. // 0x4: the colors in the array are halftone values.
  18. private int flags;
  19. private Color [] entries;
  20. //
  21. // There is no public constructor, this will be used somewhere in the
  22. // drawing code
  23. //
  24. internal ColorPalette ()
  25. {
  26. flags = 0;
  27. entries = new Color [0];
  28. }
  29. internal ColorPalette (int flags, Color[] colors) {
  30. this.flags = flags;
  31. entries = colors;
  32. }
  33. public Color [] Entries {
  34. get {
  35. return entries;
  36. }
  37. }
  38. public int Flags {
  39. get {
  40. return flags;
  41. }
  42. }
  43. /* Caller should call FreeHGlobal*/
  44. internal IntPtr getGDIPalette()
  45. {
  46. GdiColorPalette palette = new GdiColorPalette ();
  47. Color[] entries = Entries;
  48. int entry = 0;
  49. int size = Marshal.SizeOf (palette) + (Marshal.SizeOf (entry) * entries.Length);
  50. IntPtr lfBuffer = Marshal.AllocHGlobal(size);
  51. palette.Flags = Flags;
  52. palette.Count = entries.Length;
  53. int[] values = new int[palette.Count];
  54. for (int i = 0; i < values.Length; i++) {
  55. values[i] = entries[i].ToArgb();
  56. //Console.Write("{0:X} ;", values[i]);
  57. }
  58. //Console.WriteLine("pal size " + Marshal.SizeOf (palette) + " native " + NativeObject);
  59. Marshal.StructureToPtr (palette, lfBuffer, false);
  60. Marshal.Copy (values, 0, (IntPtr) (lfBuffer.ToInt32() + Marshal.SizeOf (palette)), values.Length);
  61. return lfBuffer;
  62. }
  63. internal void setFromGDIPalette(IntPtr palette)
  64. {
  65. }
  66. }
  67. }