dtable-utils
Version:
dtable common utils
110 lines (100 loc) • 4.06 kB
JavaScript
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
var _defineProperty = require('@babel/runtime/helpers/defineProperty');
var _classCallCheck = require('@babel/runtime/helpers/classCallCheck');
var _createClass = require('@babel/runtime/helpers/createClass');
var color = require('../constants/color.js');
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
var _defineProperty__default = /*#__PURE__*/_interopDefaultLegacy(_defineProperty);
var _classCallCheck__default = /*#__PURE__*/_interopDefaultLegacy(_classCallCheck);
var _createClass__default = /*#__PURE__*/_interopDefaultLegacy(_createClass);
var GradientColorUtils = /*#__PURE__*/function () {
function GradientColorUtils(_ref) {
var colorType = _ref.colorType,
max = _ref.max,
min = _ref.min;
_classCallCheck__default["default"](this, GradientColorUtils);
var newColorType = colorType || 'color_gradation_1';
var colors = color.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__default["default"](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__default["default"]({}, 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__default["default"]({}, 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;
}
}]);
}();
exports["default"] = GradientColorUtils;