A pixel art painter geared specifically at NES pixel art. Includes export for .chr binary file as well as palette and namespace data.
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

139 lines
4.0KB

  1. import GlobalEvents from "/app/js/EventCaller.js";
  2. import {NESPalette} from "/app/js/NESPalette.js";
  3. const ATTRIB_NESIDX = "nesidx";
  4. // The two attributes below MUST BOTH be in the element.
  5. const ATTRIB_PALIDX = "pidx"; // This is the palette index (0 - 3 (Tiles) 4 - 7 (Sprites))
  6. const ATTRIB_COLIDX = "cidx"; // This is the color index in the selected palette (0 - 3)
  7. const CLASS_BTN_ACTIVE = "pure-button-active";
  8. var Active_Palette_Index = 0;
  9. var Active_Color_Index = 0;
  10. function InvertRGB(hex){
  11. var h = (255 - parseInt(hex, 16)).toString(16);
  12. return (h.length < 2) ? "0" + h : h;
  13. }
  14. function InvertColor(chex, bw){
  15. bw = (bw === true);
  16. if (chex.indexOf("#") === 0){
  17. chex = chex.slice(1);
  18. }
  19. if (chex.length === 3){
  20. chex = chex[0] + chex[0] + chex[1] + chex[1] + chex[2] + chex[2];
  21. }
  22. if (chex.length !== 6){
  23. throw new ValueError("Hex color expected to be 3 or 6 characters long.");
  24. }
  25. if (bw) {
  26. var r = parseInt(chex.slice(0, 2), 16);
  27. var g = parseInt(chex.slice(2, 4), 16);
  28. var b = parseInt(chex.slice(4, 6), 16);
  29. // http://stackoverflow.com/a/3943023/112731
  30. return (r * 0.299 + g * 0.587 + b * 0.114) > 186
  31. ? '#000000' : '#FFFFFF';
  32. }
  33. return "#" + InvertRGB(chex.slice(0, 2)) + InvertRGB(chex.slice(2, 4)) + InvertRGB(chex.slice(4, 6));
  34. }
  35. function GetPaletteIndexes(el){
  36. if (el.hasAttribute(ATTRIB_PALIDX) && el.hasAttribute(ATTRIB_COLIDX)){
  37. var pi = el.getAttribute(ATTRIB_PALIDX);
  38. if (!isNaN(pi))
  39. pi = parseInt(pi);
  40. else
  41. pi = -1;
  42. var ci = el.getAttribute(ATTRIB_COLIDX);
  43. if (!isNaN(ci))
  44. ci = parseInt(ci);
  45. else
  46. ci = -1;
  47. if (pi >= 0 && pi < 4 && ci >= 0 && ci < 4){
  48. return {pi:pi, ci:ci};
  49. }
  50. }
  51. return null;
  52. }
  53. class CTRLPalettes{
  54. constructor(){
  55. this.__NESPalette = null;
  56. this.__pi = 0; // Palette index.
  57. this.__ci = 0; // Palette color index.
  58. this.__mode = 0; // 0 = Tile palette mode | 1 = Sprite palette mode.
  59. var self = this;
  60. var handle_syspalette_clicked = function(event){
  61. if (this.hasAttribute(ATTRIB_NESIDX)){
  62. var idx = parseInt(this.getAttribute(ATTRIB_NESIDX), 16);
  63. if (idx >= 0 && idx < NESPalette.SystemColor.length){
  64. console.log(idx);
  65. // TODO: Set a selected Tile/Sprite palette index to the color index clicked.
  66. }
  67. }
  68. };
  69. var elist = document.querySelectorAll("[" + ATTRIB_NESIDX + "]");
  70. elist.forEach(function(el){
  71. var idx = parseInt(el.getAttribute(ATTRIB_NESIDX), 16);
  72. if (idx >= 0 && idx < NESPalette.SystemColor.length){
  73. el.style["background-color"] = NESPalette.SystemColor[idx];
  74. el.style.color = InvertColor(NESPalette.SystemColor[idx], true);
  75. el.addEventListener("click", handle_syspalette_clicked);
  76. }
  77. });
  78. var handle_palcolor_clicked = function(event){
  79. if (this.hasAttribute(ATTRIB_PALIDX) && this.hasAttribute(ATTRIB_COLIDX)){
  80. var i = GetPaletteIndexes(this);
  81. if (i !== null){
  82. if (self.__pi !== i.pi || self.__ci !== i.ci){
  83. // TODO: Instead of storing __pi and __ci, hold the active element.
  84. }
  85. }
  86. }
  87. };
  88. var elist = document.querySelectorAll("[" + ATTRIB_PALIDX + "]");
  89. elist.forEach(function(el){
  90. if (el.hasAttribute(ATTRIB_PALIDX) && el.hasAttribute(ATTRIB_COLIDX)){
  91. el.addEventListener("click", handle_palcolor_clicked);
  92. }
  93. });
  94. }
  95. get palette(){
  96. return this.__NESPalette;
  97. }
  98. set palette(p){
  99. if (!(p instanceof NESPalette)){
  100. throw new TypeError("Expected NESPalette object instance.");
  101. }
  102. this.__NESPalette = p;
  103. var elist = document.querySelectorAll("[" + ATTRIB_PALIDX + "]");
  104. elist.forEach((function(el){
  105. if (el.hasAttribute(ATTRIB_COLIDX)){
  106. var i = GetPaletteIndexes(el);
  107. if (i !== null){
  108. var c = p.get_palette_color((this.__mode * 4) + i.pi, i.ci);
  109. el.style["background-color"] = c;
  110. el.style.color = InvertColor(c, true);
  111. }
  112. }
  113. }).bind(this));
  114. }
  115. }
  116. const instance = new CTRLPalettes();
  117. export default instance;