11import { MouseEvent } from 'react' ;
22import { ColorPreset , DrawData , SortValue } from './types' ;
3- import { hsvToRgbHex } from './utils' ;
3+ import { hsvToRgbHex , rgbHexToHsv } from './utils' ;
44
55export class CanvasController {
66 private prevHighlightIndices : number [ ] | null = null ;
@@ -14,7 +14,8 @@ export class CanvasController {
1414 public canvasRef : React . RefObject < HTMLCanvasElement > ,
1515 public columnNbr : number ,
1616 public colorPreset : ColorPreset ,
17- public columnColor : string ,
17+ public columnColor1 : string ,
18+ public columnColor2 : string ,
1819 public highlightColor : string ,
1920 ) { }
2021
@@ -43,10 +44,40 @@ export class CanvasController {
4344 return context ;
4445 }
4546
46- getColumnColor ( value : number ) {
47+ getGradientColor ( value : number ) {
48+ // eslint-disable-next-line prefer-const
49+ let [ h1 , s1 , v1 ] = rgbHexToHsv ( this . columnColor1 ) ;
50+ // eslint-disable-next-line prefer-const
51+ let [ h2 , s2 , v2 ] = rgbHexToHsv ( this . columnColor2 ) ;
52+ if ( h1 === 0 && s1 === 0 ) {
53+ h1 = h2 ;
54+ }
55+ if ( h2 === 0 && s2 === 0 ) {
56+ h2 = h1 ;
57+ }
58+
59+ // Use the shortest path in the hue circle
60+ let hDiff = h2 - h1 ;
61+ hDiff += hDiff > 180 ? - 360 : hDiff < - 180 ? 360 : 0 ;
62+
63+ const sDiff = s2 - s1 ;
64+ const vDiff = v2 - v1 ;
65+ const multiplier = value / this . columnNbr ;
66+
67+ return hsvToRgbHex (
68+ // TODO: Use additive/subtractive color mixing instead of hue shifting?
69+ ( h1 + hDiff * multiplier + 360 ) % 360 ,
70+ s1 + sDiff * multiplier ,
71+ v1 + vDiff * multiplier ,
72+ ) ;
73+ }
74+
75+ getColumnColor1 ( value : number ) {
4776 switch ( this . colorPreset ) {
4877 case ColorPreset . Custom :
49- return this . columnColor ;
78+ return this . columnColor1 ;
79+ case ColorPreset . CustomGradient :
80+ return this . getGradientColor ( value ) ;
5081 case ColorPreset . Rainbow :
5182 return hsvToRgbHex ( ( 360 * value ) / this . columnNbr , 1 , 1 ) ;
5283 }
@@ -55,6 +86,7 @@ export class CanvasController {
5586 getHighlightColor ( ) {
5687 switch ( this . colorPreset ) {
5788 case ColorPreset . Custom :
89+ case ColorPreset . CustomGradient :
5890 return this . highlightColor ;
5991 case ColorPreset . Rainbow :
6092 return '#FFFFFF' ;
@@ -192,7 +224,7 @@ export class CanvasController {
192224 ( this . canvas2dCtx . canvas . height / this . columnNbr ) * ( arr [ i ] . value + 1 ) ;
193225 const startX = width * i ;
194226
195- this . canvas2dCtx . fillStyle = color || this . getColumnColor ( arr [ i ] . value ) ;
227+ this . canvas2dCtx . fillStyle = color || this . getColumnColor1 ( arr [ i ] . value ) ;
196228 this . fillRect ( startX , 0 , width , height ) ;
197229 } ;
198230
0 commit comments