UNPKG

dtable-utils

Version:

dtable common utils

100 lines (94 loc) 3.54 kB
import _defineProperty from '@babel/runtime/helpers/defineProperty'; import _classCallCheck from '@babel/runtime/helpers/classCallCheck'; import _createClass from '@babel/runtime/helpers/createClass'; import { COLOR_GRADATION_OPTIONS } from '../constants/color.js'; var GradientColorUtils = /*#__PURE__*/function () { function GradientColorUtils(_ref) { var colorType = _ref.colorType, max = _ref.max, min = _ref.min; _classCallCheck(this, GradientColorUtils); var newColorType = colorType || 'color_gradation_1'; var colors = COLOR_GRADATION_OPTIONS[newColorType]; this.startColor = this.hexToRgb(colors[0]); this.endColor = this.hexToRgb(colors[4]); this.min = min; this.max = max; this.computeMap = {}; } return _createClass(GradientColorUtils, [{ key: "hexToRgb", value: function hexToRgb(hexCode) { var reg = /^#([0-9a-fA-f]{3}|[0-9a-fA-f]{6})$/; var nextHexCode = hexCode.toLowerCase(); if (nextHexCode && reg.test(nextHexCode)) { if (nextHexCode.length === 4) { var newHexCode = '#'; for (var i = 1; i < 4; i += 1) { newHexCode += nextHexCode.slice(i, i + 1).concat(nextHexCode.slice(i, i + 1)); } nextHexCode = newHexCode; } var hexCodeChange = []; for (var _i = 1; _i < 7; _i += 2) { hexCodeChange.push(parseInt("0x".concat(nextHexCode.slice(_i, _i + 2)))); } return "RGB(".concat(hexCodeChange.join(','), ")"); } return nextHexCode; } }, { key: "getColorGradation", value: function getColorGradation(color1, color2, value1, value2, value) { var rgb1 = color1.split(','); var r1 = parseInt(rgb1[0].split('(')[1]); var g1 = parseInt(rgb1[1]); var b1 = parseInt(rgb1[2].split(')')[0]); var rgb2 = color2.split(','); var r2 = parseInt(rgb2[0].split('(')[1]); var g2 = parseInt(rgb2[1]); var b2 = parseInt(rgb2[2].split(')')[0]); var r = Math.round(r1 - (r1 - r2) / (value1 - value2) * (value1 - value)); var g = Math.round(g1 - (g1 - g2) / (value1 - value2) * (value1 - value)); var b = Math.round(b1 - (b1 - b2) / (value1 - value2) * (value1 - value)); return "rgb(".concat(r, ", ").concat(g, ", ").concat(b, ")"); } }, { key: "getCellColor", value: function getCellColor(value) { if (this.computeMap[value]) { return this.computeMap[value]; } // eslint-disable-next-line if (value == this.min) { if (this.computeMap[value]) { this.computeMap[value] = this.startColor; } else { this.computeMap = _defineProperty({}, value, this.startColor); } return this.computeMap[value]; } // eslint-disable-next-line if (value == this.max) { if (this.computeMap[value]) { this.computeMap[value] = this.endColor; } else { this.computeMap = _defineProperty({}, value, this.endColor); } return this.computeMap[value]; } if (value > this.min && value < this.max) { var color1 = this.startColor, color2 = this.endColor, value1 = this.min, value2 = this.max; var color = this.getColorGradation(color1, color2, value1, value2, value); this.computeMap[value] = color; return this.computeMap[value]; } // value < min || value > max return null; } }]); }(); export { GradientColorUtils as default };