11import { useMemo } from "react"
22import { Formatter } from "~/utils/misc/formatter"
3+ import './meterBar.scss'
34
45const MAX_PERC = 100
56
@@ -10,10 +11,14 @@ const MAX_LEDS = 70
1011const OPACITY_BEFORE = 0.25
1112const OPACITY_AFTER = 1
1213
13- const COLOR_BAD = [ "#d94357" , "radial-gradient(#d94357 25%, #a82234)" ]
14- const COLOR_MED = [ "#e58630" , "radial-gradient(#a05714 25%, #dd781c)" ]
15- const COLOR_GUD = [ "#76B768" , "radial-gradient(#487e3c 25%, #64ae55)" ]
16- const COLORS = [ COLOR_BAD , COLOR_MED , COLOR_GUD ]
14+ // Bespoke status palette for the meter LEDs. Each tier carries the flat
15+ // label colour + the radial gradient used to render the LED itself.
16+ type StatusPalette = { label : string , gradient : string }
17+
18+ const STATUS_BAD : StatusPalette = { label : '#d94357' , gradient : 'radial-gradient(#d94357 25%, #a82234)' }
19+ const STATUS_MEDIUM : StatusPalette = { label : '#e58630' , gradient : 'radial-gradient(#a05714 25%, #dd781c)' }
20+ const STATUS_GOOD : StatusPalette = { label : '#76B768' , gradient : 'radial-gradient(#487e3c 25%, #64ae55)' }
21+ const STATUS_PALETTE = [ STATUS_BAD , STATUS_MEDIUM , STATUS_GOOD ]
1722
1823type args = {
1924 name : string
@@ -50,12 +55,12 @@ const MeterBar = ({ name, value, text, ranges, height = 40 }: args) => {
5055
5156 valueidx = getColorIdx ( scaledValue , - 1 )
5257
53- const data : { color : string [ ] , opacity : number } [ ] = [ ]
58+ const data : { palette : StatusPalette , opacity : number } [ ] = [ ]
5459 for ( let i = 0 ; i < leds ; i ++ ) {
5560 const ratio = getRatio ( i )
56- const color = getColorIdx ( ratio , scaledValue )
61+ const idx = getColorIdx ( ratio , scaledValue )
5762 const opacity = scaledValue < ratio ? OPACITY_BEFORE : OPACITY_AFTER
58- data . push ( { color : COLORS [ color ] , opacity } )
63+ data . push ( { palette : STATUS_PALETTE [ idx ] , opacity } )
5964 }
6065
6166 return { data, valueidx }
@@ -68,10 +73,10 @@ const MeterBar = ({ name, value, text, ranges, height = 40 }: args) => {
6873 {
6974 data . map ( ( d , i ) => {
7075 return < span className = 'meter-bar-led'
71- key = { i } style = { { background : d . color [ 1 ] , opacity : d . opacity } } > </ span >
76+ key = { i } style = { { background : d . palette . gradient , opacity : d . opacity } } > </ span >
7277 } )
7378 }
74- < span className = 'meter-bar-desc' style = { { color : COLORS [ valueidx ] [ 0 ] } } >
79+ < span className = 'meter-bar-desc' style = { { color : STATUS_PALETTE [ valueidx ] . label } } >
7580 { text ?? Formatter . percent ( scaledValue / MAX_PERC ) }
7681 </ span >
7782 </ div >
0 commit comments