@@ -186,6 +186,7 @@ function interpolateColor(color1, color2, factor) {
186186function 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
212217function colorFor ( value , config , rowIndex , colIndex , channels ) {
@@ -253,13 +258,28 @@ function colorFor(value, config, rowIndex, colIndex, channels) {
253258window . 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", () => {
449469document . querySelector ( "#refresh-gallery" ) . addEventListener ( "click" , renderGallery ) ;
450470
451471await renderGallery ( ) ;
452- loadPreset ( "wolfram-90" ) ;
472+ await loadPreset ( "wolfram-90" ) ;
0 commit comments