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个字符

CTRLPalettes.js 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. import {NESPalette} from "/app/js/NESPalette.js";
  2. const ATTRIB_NESIDX = "nesidx";
  3. // The two attributes below MUST BOTH be in the element.
  4. const ATTRIB_PALIDX = "pidx"; // This is the palette index (0 - 3 (Tiles) 4 - 7 (Sprites))
  5. const ATTRIB_COLIDX = "cidx"; // This is the color index in the selected palette (0 - 3)
  6. const CLASS_BTN_ACTIVE = "pure-button-active";
  7. var Active_Palette_Index = 0;
  8. var Active_Color_Index = 0;
  9. function InvertRGB(hex){
  10. var h = (255 - parseInt(hex, 16)).toString(16);
  11. return (h.length < 2) ? "0" + h : h;
  12. }
  13. function InvertColor(chex, bw){
  14. bw = (bw === true);
  15. if (chex.indexOf("#") === 0){
  16. chex = chex.slice(1);
  17. }
  18. if (chex.length === 3){
  19. chex = chex[0] + chex[0] + chex[1] + chex[1] + chex[2] + chex[2];
  20. }
  21. if (chex.length !== 6){
  22. throw new ValueError("Hex color expected to be 3 or 6 characters long.");
  23. }
  24. if (bw) {
  25. var r = parseInt(chex.slice(0, 2), 16);
  26. var g = parseInt(chex.slice(2, 4), 16);
  27. var b = parseInt(chex.slice(4, 6), 16);
  28. // http://stackoverflow.com/a/3943023/112731
  29. return (r * 0.299 + g * 0.587 + b * 0.114) > 186
  30. ? '#000000' : '#FFFFFF';
  31. }
  32. return "#" + InvertRGB(chex.slice(0, 2)) + InvertRGB(chex.slice(2, 4)) + InvertRGB(chex.slice(4, 6));
  33. }
  34. function GetPaletteIndexes(el){
  35. if (el.hasAttribute(ATTRIB_PALIDX) && el.hasAttribute(ATTRIB_COLIDX)){
  36. var pi = el.getAttribute(ATTRIB_PALIDX);
  37. if (!isNaN(pi))
  38. pi = parseInt(pi);
  39. else
  40. pi = -1;
  41. var ci = el.getAttribute(ATTRIB_COLIDX);
  42. if (!isNaN(ci))
  43. ci = parseInt(ci);
  44. else
  45. ci = -1;
  46. if (pi >= 0 && pi < 4 && ci >= 0 && ci < 4){
  47. return {pi:pi, ci:ci};
  48. }
  49. }
  50. return null;
  51. }
  52. function SetPaletteElStyle(el, c){
  53. el.style["background-color"] = c;
  54. el.style.color = InvertColor(c);
  55. }
  56. function SetColorPaletteEls(mode, pal){
  57. var elist = document.querySelectorAll("[" + ATTRIB_PALIDX + "]");
  58. elist.forEach(function(el){
  59. var i = GetPaletteIndexes(el);
  60. if (i !== null){
  61. SetPaletteElStyle(el, pal.get_palette_color((mode*4) + i.pi, i.ci));
  62. }
  63. });
  64. }
  65. function FindAndColorPalette(mode, pi, ci, pal){
  66. if ((mode == 0 && pi < 4) || (mode == 1 && pi >= 4)){
  67. var el = document.querySelector("[" + ATTRIB_PALIDX +"='" + (pi%4) + "']" +
  68. "[" + ATTRIB_COLIDX + "='" + ci + "']");
  69. if (el){
  70. SetPaletteElStyle(el, pal.get_palette_color(pi, ci));
  71. }
  72. }
  73. }
  74. class CTRLPalettes{
  75. constructor(){
  76. this.__NESPalette = null;
  77. this.__activePaletteEl = null;
  78. this.__mode = 0; // 0 = Tile palette mode | 1 = Sprite palette mode.
  79. var self = this;
  80. var handle_syspalette_clicked = function(event){
  81. if (self.__activePaletteEl !== null && this.hasAttribute(ATTRIB_NESIDX)){
  82. var idx = parseInt(this.getAttribute(ATTRIB_NESIDX), 16);
  83. if (idx >= 0 && idx < NESPalette.SystemColor.length){
  84. var i = GetPaletteIndexes(self.__activePaletteEl);
  85. if (self.__palette !== null && i !== null){
  86. self.__NESPalette.set_palette_syscolor_index(i.pi, i.ci, idx);
  87. SetPaletteElStyle(self.__activePaletteEl, NESPalette.SystemColor[idx]);
  88. }
  89. }
  90. }
  91. };
  92. var elist = document.querySelectorAll("[" + ATTRIB_NESIDX + "]");
  93. elist.forEach(function(el){
  94. var idx = parseInt(el.getAttribute(ATTRIB_NESIDX), 16);
  95. if (idx >= 0 && idx < NESPalette.SystemColor.length){
  96. SetPaletteElStyle(el, NESPalette.SystemColor[idx]);
  97. el.addEventListener("click", handle_syspalette_clicked);
  98. }
  99. });
  100. var handle_palcolor_clicked = function(event){
  101. if (this.hasAttribute(ATTRIB_PALIDX) && this.hasAttribute(ATTRIB_COLIDX)){
  102. if (this !== self.__activePaletteEl){
  103. var i = GetPaletteIndexes(this);
  104. if (i !== null){
  105. if (self.__activePaletteEl !== null){
  106. self.__activePaletteEl.classList.remove(CLASS_BTN_ACTIVE);
  107. }
  108. this.classList.add(CLASS_BTN_ACTIVE);
  109. self.__activePaletteEl = this;
  110. //self.emit("active_palette_color", i);
  111. }
  112. }
  113. }
  114. };
  115. var elist = document.querySelectorAll("[" + ATTRIB_PALIDX + "]");
  116. elist.forEach(function(el){
  117. if (el.hasAttribute(ATTRIB_PALIDX) && el.hasAttribute(ATTRIB_COLIDX)){
  118. el.addEventListener("click", handle_palcolor_clicked);
  119. }
  120. });
  121. }
  122. get palette(){
  123. return this.__NESPalette;
  124. }
  125. set palette(p){
  126. if (!(p instanceof NESPalette)){
  127. throw new TypeError("Expected NESPalette object instance.");
  128. }
  129. var self = this;
  130. var handle_palettes_changed = function(event){
  131. if (self.__NESPalette !== null){
  132. if (event.type == "ALL"){
  133. SetColorPaletteEls(self.__mode, self.__NESPalette);
  134. } else {
  135. FindAndColorPalette(self.__mode, event.pindex, event.cindex, self.__NESPalette);
  136. }
  137. }
  138. }
  139. // Disconnect listener from old palette and connect it to new palette.
  140. if (this.__NESPalette !== p){
  141. if (this.__NESPalette !== null){
  142. this.__NESPalette.unlisten("palettes_changed", handle_palettes_changed);
  143. }
  144. this.__NESPalette = p;
  145. this.__NESPalette.listen("palettes_changed", handle_palettes_changed);
  146. }
  147. var elist = document.querySelectorAll("[" + ATTRIB_PALIDX + "]");
  148. elist.forEach((function(el){
  149. if (el.hasAttribute(ATTRIB_COLIDX)){
  150. var i = GetPaletteIndexes(el);
  151. if (i !== null){
  152. SetPaletteElStyle(el, p.get_palette_color((this.__mode * 4) + i.pi, i.ci));
  153. }
  154. }
  155. }).bind(this));
  156. }
  157. }
  158. const instance = new CTRLPalettes();
  159. export default instance;