Skip to content

Commit cb727d4

Browse files
Update color gradient use
1 parent af23db4 commit cb727d4

2 files changed

Lines changed: 39 additions & 13 deletions

File tree

public/app.mjs

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ function interpolateColor(color1, color2, factor) {
186186
function getGradientColor(position, colors, positions) {
187187
if (!colors || colors.length === 0) return '#ffffff';
188188
if (colors.length === 1) return colors[0];
189+
if (!positions || positions.length !== colors.length) return colors[0];
189190

190191
// Normalize position to 0-1
191192
const t = Math.max(0, Math.min(1, position));
@@ -201,12 +202,16 @@ function getGradientColor(position, colors, positions) {
201202
}
202203
}
203204

205+
// Clamp to valid indices
206+
startIdx = Math.min(startIdx, positions.length - 2);
207+
const endIdx = Math.min(startIdx + 1, positions.length - 1);
208+
204209
const pos1 = positions[startIdx] / 100;
205-
const pos2 = positions[startIdx + 1] / 100;
210+
const pos2 = positions[endIdx] / 100;
206211
const range = pos2 - pos1;
207212
const factor = range === 0 ? 0 : (t - pos1) / range;
208213

209-
return interpolateColor(colors[startIdx], colors[startIdx + 1], factor);
214+
return interpolateColor(colors[startIdx], colors[endIdx], factor);
210215
}
211216

212217
function colorFor(value, config, rowIndex, colIndex, channels) {
@@ -253,13 +258,28 @@ function colorFor(value, config, rowIndex, colIndex, channels) {
253258
window.applyGradientToPalette = function(colors, type, positions) {
254259
if (!state.config) return;
255260
state.config.rendu = state.config.rendu || {};
261+
262+
const finalColors = colors || ["#07121f", "#ff9d4d", "#53b0ff", "#f5f7ff"];
263+
const finalPositions = positions || [0, 50, 100];
264+
265+
// Ensure colors and positions arrays have the same length
266+
const length = Math.min(finalColors.length, finalPositions.length);
267+
const syncedColors = finalColors.slice(0, length);
268+
const syncedPositions = finalPositions.slice(0, length);
269+
270+
// If arrays were mismatched, pad with defaults
271+
if (syncedColors.length < 2) {
272+
syncedColors.push("#ff9d4d", "#f5f7ff");
273+
syncedPositions.push(50, 100);
274+
}
275+
256276
state.config.rendu.gradient = {
257-
colors: colors || ["#07121f", "#ff9d4d", "#53b0ff", "#f5f7ff"],
277+
colors: syncedColors,
258278
type: type || 'linear',
259-
positions: positions || [0, 50, 100]
279+
positions: syncedPositions.map(Number)
260280
};
261281
// Keep palette as fallback
262-
state.config.rendu.palette = colors || ["#07121f", "#ff9d4d", "#53b0ff", "#f5f7ff"];
282+
state.config.rendu.palette = syncedColors;
263283
render();
264284
};
265285

@@ -449,4 +469,4 @@ document.querySelector("#export-png").addEventListener("click", () => {
449469
document.querySelector("#refresh-gallery").addEventListener("click", renderGallery);
450470

451471
await renderGallery();
452-
loadPreset("wolfram-90");
472+
await loadPreset("wolfram-90");

public/index.html

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -454,28 +454,34 @@ <h2>Configuration brute</h2>
454454

455455
function updateGradientPreview() {
456456
const colors = gradColorInputs.map(input => input.value);
457-
const positions = gradStopInputs.map(input => input.value);
457+
const positions = gradStopInputs.map(input => Number(input.value));
458+
459+
// Sort colors and positions by position value to ensure correct gradient rendering
460+
const sortedStops = positions.map((pos, i) => ({ pos, color: colors[i] }))
461+
.sort((a, b) => a.pos - b.pos);
462+
const sortedPositions = sortedStops.map(s => s.pos);
463+
const sortedColors = sortedStops.map(s => s.color);
458464

459465
let gradient = '';
460466
if (gradientType === 'linear') {
461-
gradient = `linear-gradient(90deg, ${colors.map((c, i) => `${c} ${positions[i]}%`).join(', ')})`;
467+
gradient = `linear-gradient(90deg, ${sortedColors.map((c, i) => `${c} ${sortedPositions[i]}%`).join(', ')})`;
462468
} else if (gradientType === 'radial') {
463-
gradient = `radial-gradient(circle, ${colors.map((c, i) => `${c} ${positions[i]}%`).join(', ')})`;
469+
gradient = `radial-gradient(circle, ${sortedColors.map((c, i) => `${c} ${sortedPositions[i]}%`).join(', ')})`;
464470
} else if (gradientType === 'conic') {
465-
gradient = `conic-gradient(from 0deg, ${colors.map((c, i) => `${c} ${positions[i]}%`).join(', ')})`;
471+
gradient = `conic-gradient(from 0deg, ${sortedColors.map((c, i) => `${c} ${sortedPositions[i]}%`).join(', ')})`;
466472
}
467473

468474
gradientPreview.style.background = gradient;
469475
const gradientData = {
470476
type: gradientType,
471-
colors: colors,
472-
positions: positions.map(Number)
477+
colors: sortedColors,
478+
positions: sortedPositions
473479
};
474480
localStorage.setItem('gradient-preference', JSON.stringify(gradientData));
475481

476482
// Apply gradient to cellular automata canvas
477483
if (typeof window.applyGradientToPalette === 'function') {
478-
window.applyGradientToPalette(colors, gradientType, positions.map(Number));
484+
window.applyGradientToPalette(sortedColors, gradientType, sortedPositions);
479485
}
480486
}
481487

0 commit comments

Comments
 (0)