Merge pull request #720 from redbirdly/main

Fix lightmap save loading, and rgbToArray function in both lightmap.js and fast_lightmap.js
This commit is contained in:
slweeb 2024-06-28 23:17:48 -04:00 committed by GitHub
commit 925c53db0a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 427 additions and 349 deletions

View File

@ -19,8 +19,40 @@ function getRandomElement(arr) {
return arr[Math.floor(Math.random() * arr.length)]; return arr[Math.floor(Math.random() * arr.length)];
} }
function rgbToArray(rgbString) { if (!rgbToArray) {
return rgbString.slice(4, -1).split(',').map(val => parseInt(val.trim())); function rgbToArray(colorString) {
if (typeof colorString !== 'string') {
console.error('Invalid colorString:', colorString);
return null;
}
if (colorString.startsWith('rgb')) {
// Handle RGB format
return colorString.slice(4, -1).split(',').map(val => parseInt(val.trim()));
} else if (colorString.startsWith('#')) {
// Handle HEX format
let hex = colorString.slice(1);
// Handle shorthand hex (e.g., #03F)
if (hex.length === 3) {
hex = hex.split('').map(char => char + char).join('');
}
if (hex.length !== 6) {
console.error('Invalid hex color:', colorString);
return null;
}
const r = parseInt(hex.slice(0, 2), 16);
const g = parseInt(hex.slice(2, 4), 16);
const b = parseInt(hex.slice(4, 6), 16);
return [r, g, b];
} else {
console.error('Invalid color format:', colorString);
return null;
}
}
} }
function scaleList(numbers, scale) { function scaleList(numbers, scale) {
@ -290,6 +322,12 @@ radioactiveElements.forEach(element => {
window.addEventListener('load', () => initializeLightmap(width, height)); window.addEventListener('load', () => initializeLightmap(width, height));
var originalResizeCanvas = resizeCanvas;
resizeCanvas = function(newHeight, newWidth, newPixelSize, clear) {
originalResizeCanvas(newHeight, newWidth, newPixelSize, clear);
initializeLightmap(newHeight, newWidth);
};
var originalTick = tick; var originalTick = tick;
tick = function() { tick = function() {
originalTick(); originalTick();

View File

@ -19,8 +19,40 @@ function getRandomElement(arr) {
return arr[Math.floor(Math.random() * arr.length)]; return arr[Math.floor(Math.random() * arr.length)];
} }
function rgbToArray(rgbString) { if (!rgbToArray) {
return rgbString.slice(4, -1).split(',').map(val => parseInt(val.trim())); function rgbToArray(colorString) {
if (typeof colorString !== 'string') {
console.error('Invalid colorString:', colorString);
return null;
}
if (colorString.startsWith('rgb')) {
// Handle RGB format
return colorString.slice(4, -1).split(',').map(val => parseInt(val.trim()));
} else if (colorString.startsWith('#')) {
// Handle HEX format
let hex = colorString.slice(1);
// Handle shorthand hex (e.g., #03F)
if (hex.length === 3) {
hex = hex.split('').map(char => char + char).join('');
}
if (hex.length !== 6) {
console.error('Invalid hex color:', colorString);
return null;
}
const r = parseInt(hex.slice(0, 2), 16);
const g = parseInt(hex.slice(2, 4), 16);
const b = parseInt(hex.slice(4, 6), 16);
return [r, g, b];
} else {
console.error('Invalid color format:', colorString);
return null;
}
}
} }
function scaleList(numbers, scale) { function scaleList(numbers, scale) {
@ -288,7 +320,15 @@ radioactiveElements.forEach(element => {
elements[element].tick = glowRadiationColor; elements[element].tick = glowRadiationColor;
}); });
window.addEventListener('load', () => initializeLightmap(width, height)); window.addEventListener('load', () => {
initializeLightmap(width, height);
var originalResizeCanvas = resizeCanvas;
resizeCanvas = function(newHeight, newWidth, newPixelSize, clear) {
originalResizeCanvas(newHeight, newWidth, newPixelSize, clear);
initializeLightmap(newHeight, newWidth);
};
});
var originalTick = tick; var originalTick = tick;
tick = function() { tick = function() {