UNPKG

@antv/g2plot

Version:

G2 Plot, a market of plots built with the Grammar of Graphics'

45 lines 1.65 kB
import { rgb2arr } from '../../../../../util/color'; import * as _ from '@antv/util'; export function mappingColor(band, gray) { var reflect; _.each(band, function (b) { var map = b; if (gray >= map.from && gray < map.to) { reflect = map.color; } }); return reflect; } /** 自动调整label颜色 */ export function autoAdjustColor(label, shape) { var labelRange = label.getBBox(); var shapeRange = shape.getBBox(); if (labelRange.minY >= shapeRange.minY && labelRange.maxY <= shapeRange.maxY) { var shapeColor = shape.attr('fill'); var shapeOpacity = shape.attr('opacity') ? shape.attr('opacity') : 1; var rgb = rgb2arr(shapeColor); var gray = Math.round(rgb[0] * 0.299 + rgb[1] * 0.587 + rgb[2] * 0.114) / shapeOpacity; var colorBand = [ { from: 0, to: 85, color: 'white' }, { from: 85, to: 170, color: '#F6F6F6' }, { from: 170, to: 255, color: 'black' }, ]; var reflect = mappingColor(colorBand, gray); label.attr('fill', reflect); if (reflect !== 'black') { label.attr('stroke', null); label.attr('lineWidth', 0); } else { label.attr('stroke', 'white'); label.attr('lineWidth', 2); } } else if (labelRange.maxY < shapeRange.minY) { // 非 shape 范围内的 label 需要考虑主题背景 var theme = this.get('theme'); var labelTextColor = _.get(theme, 'label.textStyle.fill', 'black'); label.attr('fill', labelTextColor); } } //# sourceMappingURL=index.js.map