| import {EventCaller} from "/app/js/EventCaller.js" | import {EventCaller} from "/app/js/EventCaller.js" | ||||
| /** | |||||
| * Object for manipulating the eight NES palettes. | |||||
| * @extends EventCaller | |||||
| */ | |||||
| export class NESPalette extends EventCaller{ | export class NESPalette extends EventCaller{ | ||||
| constructor(){ | constructor(){ | ||||
| super(); | super(); | ||||
| ]; | ]; | ||||
| } | } | ||||
| /** | |||||
| * Sets one or all of the eight color palettes to the values given. By default, function | |||||
| * assumes the given array is for all eight palettes (or 25 total color indexes, 3 per palette | |||||
| * and 1 background/transparency color used by ALL palettes). | |||||
| * If a single palette is being set, the array must only contain 3 entries. | |||||
| * @param {Array} apci - Array of color indexes to store into the palette(s) | |||||
| * @param {number} [p=8] - Zero-based index of the palette being set. Any value outside the range of 0 - 7 will set ALL palettes. | |||||
| * @returns {this} | |||||
| */ | |||||
| set_palette(apci, p=8){ | set_palette(apci, p=8){ | ||||
| if (typeof(p) != 'number') | if (typeof(p) != 'number') | ||||
| throw new TypeError("First argument expected to be a number."); | throw new TypeError("First argument expected to be a number."); | ||||
| return this; | return this; | ||||
| } | } | ||||
| /** | |||||
| * Sets a palette's color index value to a given system color index. | |||||
| * NOTE: Setting palette color index 0 for ANY palette changes that index for ALL palettes. | |||||
| * @param {number} p - The index of the palette being set. | |||||
| * @param {number} pci - The palette color index (0 - 3) to set. | |||||
| * @param {number} sci - The system color index (0 - 63) value to set to. | |||||
| * @returns {this} | |||||
| */ | |||||
| set_palette_syscolor_index(p, pci, sci){ | set_palette_syscolor_index(p, pci, sci){ | ||||
| if (typeof(p) != 'number' || typeof(pci) != 'number' || typeof(sci) != 'number') | if (typeof(p) != 'number' || typeof(pci) != 'number' || typeof(sci) != 'number') | ||||
| throw new TypeError("Palette, palette color, and system color index expected to be numbers."); | throw new TypeError("Palette, palette color, and system color index expected to be numbers."); | ||||
| return this; | return this; | ||||
| } | } | ||||
| /** | |||||
| * Returns the system color index at the given palette color index. | |||||
| * @param {number} p - The index (0 - 7) of the palette. | |||||
| * @param {number} pci - The palette color index (0 - 3). | |||||
| * @returns {number} - The index of the system color used. | |||||
| */ | |||||
| get_palette_syscolor_index(p, pci){ | get_palette_syscolor_index(p, pci){ | ||||
| if (typeof(p) != 'number' || typeof(pci) != 'number') | if (typeof(p) != 'number' || typeof(pci) != 'number') | ||||
| throw new TypeError("Palette and color index expected to be numbers."); | throw new TypeError("Palette and color index expected to be numbers."); | ||||
| return (pci === 0) ? this.__BGColor : this.__palette[(p*3)+(pci-1)]; | return (pci === 0) ? this.__BGColor : this.__palette[(p*3)+(pci-1)]; | ||||
| } | } | ||||
| /** | |||||
| * Returns a hex string color value used by the NES system at the index stored at the given | |||||
| * palette color index. | |||||
| * @param {number} p - The index (0 - 7) of the palette. | |||||
| * @param {number} pci - The palette color index (0 - 3). | |||||
| * @returns {string} | |||||
| */ | |||||
| get_palette_color(p, pci){ | get_palette_color(p, pci){ | ||||
| if (typeof(p) != 'number' || typeof(pci) != 'number') | if (typeof(p) != 'number' || typeof(pci) != 'number') | ||||
| throw new TypeError("Palette and color index expected to be numbers."); | throw new TypeError("Palette and color index expected to be numbers."); | ||||
| return NESPalette.SystemColor(this.get_palette_syscolor_index(p, pci)); | return NESPalette.SystemColor(this.get_palette_syscolor_index(p, pci)); | ||||
| } | } | ||||
| /** | |||||
| * Generates a small 6502 assembly block string containing the current palette data. | |||||
| * @param {string} [memname="PaletteData"] The label named under which to store the data. | |||||
| * @returns {string} | |||||
| */ | |||||
| to_asm(memname="PaletteData"){ | to_asm(memname="PaletteData"){ | ||||
| var NumToHex=function(n){ | var NumToHex=function(n){ | ||||
| var h = n.toString(16); | var h = n.toString(16); | ||||
| // NES Palette color information comes from the following site... | // NES Palette color information comes from the following site... | ||||
| // http://www.thealmightyguru.com/Games/Hacking/Wiki/index.php/NES_Palette | // http://www.thealmightyguru.com/Games/Hacking/Wiki/index.php/NES_Palette | ||||
| /** | |||||
| * Hex string color values representing the NES system palette. | |||||
| */ | |||||
| NESPalette.SystemColor = [ | NESPalette.SystemColor = [ | ||||
| "#7C7C7C", | "#7C7C7C", | ||||
| "#0000FC", | "#0000FC", |