Advertisement
Chronos_Ouroboros

Styff

Jun 7th, 2015
536
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.95 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Drawing;
  5. using System.Linq;
  6. using System.Text;
  7.  
  8. namespace ModdingUtils {
  9. namespace GFX {
  10. public class Palette256Colours {
  11. Color [] colours = new Color [256];
  12.  
  13. /// <summary>
  14. ///
  15. /// </summary>
  16. /// <param name="newColours">The array of colours to set the palette to</param>
  17. public Palette256Colours (Color [] newColours) {
  18. for (int i = 0; i < 256; i++) {
  19. if (i > newColours.Length - 1)
  20. colours [i] = Color.Black;
  21. else
  22. colours [i] = Color.FromArgb (255, newColours [i].R, newColours [i].G, newColours [i].B);
  23. }
  24. }
  25.  
  26. public Color this [int index] {
  27. get {
  28. return colours [index];
  29. }
  30. set {
  31. colours [index] = value;
  32. }
  33. }
  34. }
  35.  
  36. public class IMG256Colours : IEnumerable, IEnumerator {
  37. int position = -1;
  38.  
  39. private int _width;
  40. private int _height;
  41. private Palette256Colours _palette;
  42.  
  43. private byte [] _colours;
  44.  
  45. //IEnumerator and IEnumerable require these methods.
  46. public IEnumerator GetEnumerator () {
  47. return (IEnumerator) this;
  48. }
  49.  
  50. //IEnumerator
  51. public bool MoveNext () {
  52. position++;
  53. return (position < _colours.Length);
  54. }
  55.  
  56. //IEnumerable
  57. public void Reset () { position = 0; }
  58.  
  59. //IEnumerable
  60. public object Current {
  61. get { return _colours [position]; }
  62. }
  63.  
  64. /// <summary>
  65. /// Gets the image's width
  66. /// </summary>
  67. public int width {
  68. get {
  69. return _width;
  70. } set {
  71. _width = value;
  72. _colours = new byte [_width * _height];
  73. }
  74. }
  75.  
  76. /// <summary>
  77. /// Gets the image's height
  78. /// </summary>
  79. public int height {
  80. get {
  81. return _height;
  82. } set {
  83. _height = value;
  84. _colours = new byte [_width * _height];
  85. }
  86. }
  87.  
  88. /// <summary>
  89. /// Erases the contents of the picture. (Sets all pixels to 0)
  90. /// </summary>
  91. public void Erase () {
  92. for (int i = 0; i < _colours.Length; i++)
  93. _colours [i] = 0;
  94. }
  95.  
  96. /// <summary>
  97. /// Gets or sets the image's palette
  98. /// </summary>
  99. public Palette256Colours palette {
  100. get {
  101. return _palette;
  102. } set {
  103. _palette = value;
  104. }
  105. }
  106.  
  107. public IMG256Colours (int newWidth, int newHeight, Palette256Colours newPalette) {
  108. _width = newWidth;
  109. _height = newHeight;
  110. _palette = newPalette;
  111.  
  112. _colours = new byte [newWidth * newHeight];
  113. }
  114.  
  115. ~IMG256Colours () {
  116. _width = 0;
  117. _height = 0;
  118. }
  119.  
  120. public byte this [int index] {
  121. get {
  122. return _colours [index];
  123. } set {
  124. _colours [index] = value;
  125. }
  126. }
  127.  
  128. public Bitmap ToBitmap () {
  129. Random randomizer = new Random ();
  130. Bitmap bitmap = new Bitmap (_width, _height);
  131.  
  132. for (int i = 0; i < _width * _height; i++) {
  133. bitmap.SetPixel (i % _width, i / _width, _palette [_colours [i]]);
  134. }
  135.  
  136. return bitmap;
  137. }
  138. }
  139. }
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement