@@ -198,11 +198,37 @@ async function sampleImageColors(src: string): Promise<SampleStats | null> {
198198 }
199199 }
200200
201- if ( bestBucket . weight > 0 ) {
201+ const sortedBuckets = [ ...buckets ]
202+ . map ( ( bucket , index ) => ( { ...bucket , index } ) )
203+ . sort ( ( a , b ) => b . weight - a . weight ) ;
204+
205+ const primaryBucket = sortedBuckets [ 0 ] ;
206+ const secondaryBucket = sortedBuckets . find ( ( bucket , idx ) => {
207+ if ( idx === 0 ) return false ;
208+ if ( bucket . weight <= 0 || ! primaryBucket || primaryBucket . weight === 0 ) return false ;
209+
210+ const primaryColor = {
211+ r : Math . round ( primaryBucket . r / primaryBucket . weight ) ,
212+ g : Math . round ( primaryBucket . g / primaryBucket . weight ) ,
213+ b : Math . round ( primaryBucket . b / primaryBucket . weight ) ,
214+ } ;
215+ const secondaryColor = {
216+ r : Math . round ( bucket . r / bucket . weight ) ,
217+ g : Math . round ( bucket . g / bucket . weight ) ,
218+ b : Math . round ( bucket . b / bucket . weight ) ,
219+ } ;
220+ const primaryHsl = rgbToHsl ( primaryColor . r , primaryColor . g , primaryColor . b ) ;
221+ const secondaryHsl = rgbToHsl ( secondaryColor . r , secondaryColor . g , secondaryColor . b ) ;
222+ const hueGap = hueDistance ( primaryHsl . h , secondaryHsl . h ) ;
223+ const ratio = bucket . weight / primaryBucket . weight ;
224+ return hueGap >= 20 && ratio >= 0.12 ;
225+ } ) ;
226+
227+ if ( primaryBucket && primaryBucket . weight > 0 ) {
202228 dominant = {
203- r : Math . round ( bestBucket . r / bestBucket . weight ) ,
204- g : Math . round ( bestBucket . g / bestBucket . weight ) ,
205- b : Math . round ( bestBucket . b / bestBucket . weight ) ,
229+ r : Math . round ( primaryBucket . r / primaryBucket . weight ) ,
230+ g : Math . round ( primaryBucket . g / primaryBucket . weight ) ,
231+ b : Math . round ( primaryBucket . b / primaryBucket . weight ) ,
206232 } ;
207233 } else {
208234 dominant = {
@@ -221,7 +247,15 @@ async function sampleImageColors(src: string): Promise<SampleStats | null> {
221247 }
222248 : dominantColor ;
223249
224- const accent = vibrantColor ?? dominantColor ;
250+ const secondaryColor = secondaryBucket && secondaryBucket . weight > 0
251+ ? {
252+ r : Math . round ( secondaryBucket . r / secondaryBucket . weight ) ,
253+ g : Math . round ( secondaryBucket . g / secondaryBucket . weight ) ,
254+ b : Math . round ( secondaryBucket . b / secondaryBucket . weight ) ,
255+ }
256+ : null ;
257+
258+ const accent = secondaryColor ?? vibrantColor ?? dominantColor ;
225259 const dominantHsl = rgbToHsl ( dominantColor . r , dominantColor . g , dominantColor . b ) ;
226260 const accentHsl = rgbToHsl ( accent . r , accent . g , accent . b ) ;
227261
0 commit comments