A pixel art painter geared specifically at NES pixel art. Includes export for .chr binary file as well as palette and namespace data.
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

189 行
4.4KB

  1. export class NESPalette {
  2. constructor(){
  3. this.__BGColor = 63; // Index to the background color ALL palettes MUST share.
  4. this.__palette = [
  5. // Tile/Background Palettes
  6. 0,0,0,
  7. 0,0,0,
  8. 0,0,0,
  9. 0,0,0,
  10. // Sprite Palettes
  11. 0,0,0,
  12. 0,0,0,
  13. 0,0,0,
  14. 0,0,0
  15. ];
  16. }
  17. set_palette(apci, p=8){
  18. if (typeof(p) != 'number')
  19. throw new TypeError("First argument expected to be a number.");
  20. if (!(apci instanceof Array))
  21. throw new TypeError("Expected an array of color index values.");
  22. if (p < 0 || p >= 8){ // Setting ALL palettes!
  23. if (apci.length != 25)
  24. throw new RangeError("Color array must contain 25 color values to fill all palettes.");
  25. this.__BGColor = idx[0];
  26. for (var i=0; i < 24; i++){
  27. if (typeof(apci[i+1]) == 'number'){
  28. this.__palette[i] = apci[i+1]
  29. }
  30. }
  31. } else { // Setting a specific palette.
  32. if (apci.length != 3)
  33. throw new RangeError("Color array must contain three color values.");
  34. p *= 3;
  35. for (var i=0; i < 4; i++){
  36. if (typeof(apci[i]) === 'number'){
  37. this.__palette[p+i] = apci[i];
  38. }
  39. }
  40. }
  41. return this;
  42. }
  43. set_palette_syscolor_index(p, pci, sci){
  44. if (typeof(p) != 'number' || typeof(pci) != 'number' || typeof(sci) != 'number')
  45. throw new TypeError("Palette, palette color, and system color index expected to be numbers.");
  46. if (p < 0 || p >= 8){
  47. throw new RangeError("Palette index is out of bounds.");
  48. }
  49. if (pci < 0 || pci >= 4){
  50. throw new RangeError("Palette color index is out of bounds.");
  51. }
  52. if (sci < 0 || sci >= 64){
  53. throw new RangeError("System color index is out of bounds.");
  54. }
  55. if (pci == 0)
  56. this.__BGColor = sci;
  57. else
  58. this.__palette[(p*3) + (pci-1)] = sci;
  59. return this;
  60. }
  61. get_palette_syscolor_index(p, pci){
  62. if (typeof(p) != 'number' || typeof(pci) != 'number')
  63. throw new TypeError("Palette and color index expected to be numbers.");
  64. if (p < 0 || p >= 8){
  65. throw new RangeError("Palette index is out of bounds.");
  66. }
  67. if (pci < 0 || pci >= 4){
  68. throw new RangeError("Palette color index is out of bounds.");
  69. }
  70. return (pci === 0) ? this.__BGColor : this.__palette[(p*3)+(pci-1)];
  71. }
  72. get_palette_color(p, pci){
  73. if (typeof(p) != 'number' || typeof(pci) != 'number')
  74. throw new TypeError("Palette and color index expected to be numbers.");
  75. if (p < 0 || p >= 8){
  76. throw new RangeError("Palette index is out of bounds.");
  77. }
  78. if (pci < 0 || pci >= 4){
  79. throw new RangeError("Palette color index is out of bounds.");
  80. }
  81. return NESPalette.SystemColor(this.get_palette_syscolor_index(p, pci));
  82. }
  83. to_asm(memname="PaletteData"){
  84. var NumToHex=function(n){
  85. var h = n.toString(16);
  86. if (h.length %2)
  87. h = '0' + h;
  88. return '$' + h;
  89. };
  90. var BGHex = NumToHex(this.__BGColor);
  91. var s = memname + ":\n\t.db ";
  92. // Storing background palette data.
  93. for (var i=0; i < 12; i++){
  94. if (i % 3 == 0)
  95. s += ((i == 0) ? "" : " ") + BGHex;
  96. s += " " + NumToHex(this.__palette[i]);
  97. }
  98. s += "\t; Background palette data.\n\t.db ";
  99. // Storing foreground palette data.
  100. for (var i=12; i < 24; i++){
  101. if (i % 3 == 0)
  102. s += ((i == 12) ? "" : " ") + BGHex;
  103. s += " " + NumToHex(this.__palette[i]);
  104. }
  105. s += "\t; Foreground palette data.";
  106. return s;
  107. }
  108. }
  109. // NES Palette color information comes from the following site...
  110. // http://www.thealmightyguru.com/Games/Hacking/Wiki/index.php/NES_Palette
  111. NESPalette.SystemColor = [
  112. "#7C7C7C",
  113. "#0000FC",
  114. "#0000BC",
  115. "#4428BC",
  116. "#940084",
  117. "#A80020",
  118. "#A81000",
  119. "#881400",
  120. "#503000",
  121. "#007800",
  122. "#006800",
  123. "#005800",
  124. "#004058",
  125. "#000000",
  126. "#000000",
  127. "#000000",
  128. "#BCBCBC",
  129. "#0078F8",
  130. "#0058F8",
  131. "#6844FC",
  132. "#D800CC",
  133. "#E40058",
  134. "#F83800",
  135. "#E45C10",
  136. "#AC7C00",
  137. "#00B800",
  138. "#00A800",
  139. "#00A844",
  140. "#008888",
  141. "#000000",
  142. "#000000",
  143. "#000000",
  144. "#F8F8F8",
  145. "#3CBCFC",
  146. "#6888FC",
  147. "#9878F8",
  148. "#F878F8",
  149. "#F85898",
  150. "#F87858",
  151. "#FCA044",
  152. "#F8B800",
  153. "#B8F818",
  154. "#58D854",
  155. "#58F898",
  156. "#00E8D8",
  157. "#787878",
  158. "#000000",
  159. "#000000",
  160. "#FCFCFC",
  161. "#A4E4FC",
  162. "#B8B8F8",
  163. "#D8B8F8",
  164. "#F8B8F8",
  165. "#F8A4C0",
  166. "#F0D0B0",
  167. "#FCE0A8",
  168. "#F8D878",
  169. "#D8F878",
  170. "#B8F8B8",
  171. "#B8F8D8",
  172. "#00FCFC",
  173. "#F8D8F8",
  174. "#000000",
  175. "#000000"
  176. ];