ソースを参照

Modified NESPalette to accept color indexes as two character hex strings.

dev-bank
Bryan Miller 5年前
コミット
0d9058b70a
2個のファイルの変更33行の追加10行の削除
  1. +1
    -1
      app/js/main.js
  2. +32
    -9
      app/js/models/NESPalette.js

+ 1
- 1
app/js/main.js ファイルの表示

@@ -34,7 +34,7 @@ function initialize(DOC){
var palette = new NESPalette();
// TODO: At least define a more useful set of palettes. As it is, these are just random.
palette.set_palette([
44,
"01",
11,12,13,
54,23,43,
23,18,11,

+ 32
- 9
app/js/models/NESPalette.js ファイルの表示

@@ -32,6 +32,23 @@ export default class NESPalette extends EventCaller{
* @returns {this}
*/
set_palette(apci, p=8){
var StoreColorValue = (i, v) => {
if (typeof(v) == 'number'){
if (i >= 0)
this.__palette[i] = v
else
this.__BGColor = v;
} else if (typeof(v) == 'string' && v.length == 2){
var c = parseInt(v, 16);
if (!isNaN(c)){
if (i >= 0)
this.__palette[i] = c;
else
this.__BGColor = c;
}
}
};

if (typeof(p) != 'number')
throw new TypeError("First argument expected to be a number.");
if (!(apci instanceof Array))
@@ -39,20 +56,16 @@ export default class NESPalette extends EventCaller{
if (p < 0 || p >= 8){ // Setting ALL palettes!
if (apci.length != 25)
throw new RangeError("Color array must contain 25 color values to fill all palettes.");
this.__BGColor = apci[0];
StoreColorValue(-1, apci[0]);
for (var i=0; i < 24; i++){
if (typeof(apci[i+1]) == 'number'){
this.__palette[i] = apci[i+1]
}
StoreColorValue(i, apci[i+1]);
}
} else { // Setting a specific palette.
if (apci.length != 3)
throw new RangeError("Color array must contain three color values.");
p *= 3;
for (var i=0; i < 4; i++){
if (typeof(apci[i]) === 'number'){
this.__palette[p+i] = apci[i];
}
StoreColorValue(p+i, apci[i]);
}
}
this.emit("palettes_changed", {type:"ALL"});
@@ -76,6 +89,10 @@ export default class NESPalette extends EventCaller{
if (pci < 0 || pci >= 4){
throw new RangeError("Palette color index is out of bounds.");
}
if (typeof(sci) == "string" && sci.length == 2)
sci = parseInt(sci, 16);
if (isNaN(sci))
throw new TypeError("System Color Index expected to be a number of hex value string.");
if (sci < 0 || sci >= 64){
throw new RangeError("System color index is out of bounds.");
}
@@ -93,9 +110,10 @@ export default class NESPalette extends EventCaller{
* 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).
* @param {boolean} [ashex=false] - If true, will return the index as a two character hex string.
* @returns {number} - The index of the system color used.
*/
get_palette_syscolor_index(p, pci){
get_palette_syscolor_index(p, pci, ashex=false){
if (typeof(p) != 'number' || typeof(pci) != 'number')
throw new TypeError("Palette and color index expected to be numbers.");
if (p < 0 || p >= 8){
@@ -104,7 +122,12 @@ export default class NESPalette extends EventCaller{
if (pci < 0 || pci >= 4){
throw new RangeError("Palette color index is out of bounds.");
}
return (pci === 0) ? this.__BGColor : this.__palette[(p*3)+(pci-1)];
var i = (pci === 0) ? this.__BGColor : this.__palette[(p*3)+(pci-1)];
if (ashex){
i = i.toString(16);
i = ((i.length < 2) ? "0" : "") + i;
}
return i;
}

/**

読み込み中…
キャンセル
保存