Optimize single stat text color based on background color
parent
41a989288c
commit
c1897cc36c
|
@ -3,7 +3,11 @@ import _ from 'lodash'
|
||||||
import classnames from 'classnames'
|
import classnames from 'classnames'
|
||||||
import lastValues from 'shared/parsing/lastValues'
|
import lastValues from 'shared/parsing/lastValues'
|
||||||
|
|
||||||
import {SMALL_CELL_HEIGHT} from 'src/shared/graphs/helpers'
|
import {SMALL_CELL_HEIGHT} from 'shared/graphs/helpers'
|
||||||
|
import {isBackgroundLight} from 'shared/constants/colorOperations'
|
||||||
|
|
||||||
|
const darkText = '#292933'
|
||||||
|
const lightText = '#ffffff'
|
||||||
|
|
||||||
class SingleStat extends PureComponent {
|
class SingleStat extends PureComponent {
|
||||||
render() {
|
render() {
|
||||||
|
@ -22,18 +26,26 @@ class SingleStat extends PureComponent {
|
||||||
|
|
||||||
const precision = 100.0
|
const precision = 100.0
|
||||||
const roundedValue = Math.round(+lastValue * precision) / precision
|
const roundedValue = Math.round(+lastValue * precision) / precision
|
||||||
|
let bgColor = null
|
||||||
|
let textColor = null
|
||||||
|
let className = 'single-stat'
|
||||||
|
|
||||||
const sortedColors = _.sortBy(colors, color => Number(color.value))
|
if (colors && colors.length > 0) {
|
||||||
const nearestCrossedThreshold = sortedColors
|
className = 'single-stat single-stat--colored'
|
||||||
.filter(color => lastValue > color.value)
|
const sortedColors = _.sortBy(colors, color => Number(color.value))
|
||||||
.pop()
|
const nearestCrossedThreshold = sortedColors
|
||||||
|
.filter(color => lastValue > color.value)
|
||||||
|
.pop()
|
||||||
|
|
||||||
const bgColor = nearestCrossedThreshold ? nearestCrossedThreshold.hex : null
|
bgColor = nearestCrossedThreshold
|
||||||
const textColor = colors ? '#ffffff' : null
|
? nearestCrossedThreshold.hex
|
||||||
|
: '#292933'
|
||||||
|
textColor = isBackgroundLight(bgColor) ? darkText : lightText
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div
|
<div
|
||||||
className="single-stat"
|
className={className}
|
||||||
style={{backgroundColor: bgColor, color: textColor}}
|
style={{backgroundColor: bgColor, color: textColor}}
|
||||||
>
|
>
|
||||||
<span
|
<span
|
||||||
|
|
|
@ -91,15 +91,16 @@
|
||||||
height: 100% !important;
|
height: 100% !important;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
.single-stat.single-stat--colored {
|
||||||
|
transition: background-color 0.25s ease, color 0.25s ease;
|
||||||
|
}
|
||||||
.single-stat--value {
|
.single-stat--value {
|
||||||
position: absolute;
|
position: absolute;
|
||||||
top: calc(50% - 15px);
|
top: calc(50% - 15px);
|
||||||
left: 50%;
|
left: 50%;
|
||||||
transform: translate(-50%,-50%);
|
transform: translate(-50%,-50%);
|
||||||
width: calc(100% - 32px);
|
width: calc(100% - 32px);
|
||||||
// overflow: hidden;
|
|
||||||
text-align: center;
|
text-align: center;
|
||||||
// text-overflow: ellipsis;
|
|
||||||
font-size: 54px;
|
font-size: 54px;
|
||||||
line-height: 54px;
|
line-height: 54px;
|
||||||
font-weight: 300;
|
font-weight: 300;
|
||||||
|
|
Loading…
Reference in New Issue