11import { Formatter } from "~/utils/misc/formatter"
22
33const MAX_PERC = 100
4- const LED_TOTAL = 70
4+
5+ const LEDS_PER_PIXEL = 0.035
6+ const MIN_LEDS = 10
7+ const MAX_LEDS = 70
58
69const OPACITY_BEFORE = 0.25
710const OPACITY_AFTER = 1
@@ -20,40 +23,45 @@ type args = {
2023 reverse ?: boolean
2124}
2225
23- const MeterBar = ( { name, value, text, ranges, height = 40 , reverse = false } : args ) => {
26+ const MeterBar = ( { name, value, text, ranges, height = 40 } : args ) => {
27+ const leds = Math . min ( Math . max ( LEDS_PER_PIXEL * window . innerWidth , MIN_LEDS ) , MAX_LEDS )
28+
2429 value = value * MAX_PERC
30+ let valueidx = 0
2531
2632 function getRatio ( index : number ) {
27- return Math . floor ( MAX_PERC * index / LED_TOTAL )
33+ return Math . floor ( MAX_PERC * index / leds )
2834 }
2935
3036 function getLedOpacity ( ratio : number ) {
3137 return value < ratio ? OPACITY_BEFORE : OPACITY_AFTER
3238 }
3339
34- function getLedColor ( ratio : number ) {
40+ function getColorIdx ( ratio : number ) : number {
41+ if ( ratio < value ) {
42+ return valueidx
43+ }
3544 let coloridx = null
3645 for ( let i = 0 ; i < ranges . length ; i ++ ) {
37- if ( ratio < ranges [ i ] ) {
46+ if ( ranges [ i ] >= ratio ) {
3847 coloridx = i
3948 break
4049 }
4150 }
42- if ( coloridx == null ) coloridx = ranges . length
43- return reverse ? COLORS [ ranges . length - coloridx ] : COLORS [ coloridx ]
51+ if ( coloridx == null ) {
52+ coloridx = ranges . length
53+ }
54+ return coloridx
4455 }
4556
46- const valuecolor = getLedColor ( value )
47- if ( text == null ) {
48- text = Formatter . percent ( value / 100 , 0 )
49- }
57+ valueidx = getColorIdx ( value )
5058
5159 let data : { color : string [ ] , opacity : number } [ ] = [ ]
52- for ( let i = 0 ; i < LED_TOTAL ; i ++ ) {
60+ for ( let i = 0 ; i < leds ; i ++ ) {
5361 const ratio = getRatio ( i )
54- const color = getLedColor ( ratio )
62+ const color = getColorIdx ( ratio )
5563 const opacity = getLedOpacity ( ratio )
56- data . push ( { color, opacity } )
64+ data . push ( { color : COLORS [ color ] , opacity } )
5765 }
5866
5967 return (
@@ -66,7 +74,9 @@ const MeterBar = ({ name, value, text, ranges, height = 40, reverse = false }: a
6674 key = { i } style = { { background : d . color [ 1 ] , opacity : d . opacity } } > </ span >
6775 } )
6876 }
69- < span className = 'meter-bar-desc' style = { { color : valuecolor [ 0 ] } } > { text } </ span >
77+ < span className = 'meter-bar-desc' style = { { color : COLORS [ valueidx ] [ 0 ] } } >
78+ { text ?? Formatter . percent ( value / MAX_PERC ) }
79+ </ span >
7080 </ div >
7181 </ div >
7282 ) ;
0 commit comments