ColorPalette.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. //
  10. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  11. //
  12. // Permission is hereby granted, free of charge, to any person obtaining
  13. // a copy of this software and associated documentation files (the
  14. // "Software"), to deal in the Software without restriction, including
  15. // without limitation the rights to use, copy, modify, merge, publish,
  16. // distribute, sublicense, and/or sell copies of the Software, and to
  17. // permit persons to whom the Software is furnished to do so, subject to
  18. // the following conditions:
  19. //
  20. // The above copyright notice and this permission notice shall be
  21. // included in all copies or substantial portions of the Software.
  22. //
  23. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  27. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  28. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  29. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  30. //
  31. using System;
  32. using System.Drawing;
  33. using System.Runtime.InteropServices;
  34. namespace System.Drawing.Imaging
  35. {
  36. public sealed class ColorPalette {
  37. // 0x1: the color values in the array contain alpha information
  38. // 0x2: the color values are grayscale values.
  39. // 0x4: the colors in the array are halftone values.
  40. private int flags;
  41. private Color [] entries;
  42. //
  43. // There is no public constructor, this will be used somewhere in the
  44. // drawing code
  45. //
  46. internal ColorPalette ()
  47. {
  48. flags = 0;
  49. entries = new Color [0];
  50. }
  51. internal ColorPalette (int flags, Color[] colors) {
  52. this.flags = flags;
  53. entries = colors;
  54. }
  55. public Color [] Entries {
  56. get {
  57. return entries;
  58. }
  59. }
  60. public int Flags {
  61. get {
  62. return flags;
  63. }
  64. }
  65. #if !TARGET_JVM
  66. /* Caller should call FreeHGlobal*/
  67. internal IntPtr getGDIPalette()
  68. {
  69. GdiColorPalette palette = new GdiColorPalette ();
  70. Color[] entries = Entries;
  71. int entry = 0;
  72. int size = Marshal.SizeOf (palette) + (Marshal.SizeOf (entry) * entries.Length);
  73. IntPtr lfBuffer = Marshal.AllocHGlobal(size);
  74. palette.Flags = Flags;
  75. palette.Count = entries.Length;
  76. int[] values = new int[palette.Count];
  77. for (int i = 0; i < values.Length; i++) {
  78. values[i] = entries[i].ToArgb();
  79. }
  80. Marshal.StructureToPtr (palette, lfBuffer, false);
  81. Marshal.Copy (values, 0, (IntPtr) (lfBuffer.ToInt64() + Marshal.SizeOf (palette)), values.Length);
  82. return lfBuffer;
  83. }
  84. internal void setFromGDIPalette (IntPtr palette)
  85. {
  86. IntPtr ptr = palette;
  87. int cnt, color;
  88. int offset;
  89. flags = Marshal.ReadInt32 (ptr); ptr = (IntPtr) (ptr.ToInt64() + 4);
  90. cnt = Marshal.ReadInt32 (ptr); ptr = (IntPtr) (ptr.ToInt64() + 4);
  91. entries = new Color [cnt];
  92. offset = 0;
  93. for (int i = 0; i < cnt; i++) {
  94. color = Marshal.ReadInt32 (ptr, offset);
  95. entries[i] = Color.FromArgb (color);
  96. offset += 4;
  97. }
  98. }
  99. #endif
  100. }
  101. }