UNPKG

5.27 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6
7var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
8
9var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
10
11var _tinycolor = require('tinycolor2');
12
13var _tinycolor2 = _interopRequireDefault(_tinycolor);
14
15function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
16
17function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
18
19var Color = function () {
20 function Color(input) {
21 var _this = this;
22
23 _classCallCheck(this, Color);
24
25 this.initRgb = function () {
26 var _color$toRgb = _this.color.toRgb(),
27 r = _color$toRgb.r,
28 g = _color$toRgb.g,
29 b = _color$toRgb.b;
30
31 _this.redValue = r;
32 _this.greenValue = g;
33 _this.blueValue = b;
34 };
35
36 this.initHsb = function () {
37 var _color$toHsv = _this.color.toHsv(),
38 h = _color$toHsv.h,
39 s = _color$toHsv.s,
40 v = _color$toHsv.v;
41
42 _this.hueValue = h;
43 _this.saturationValue = s;
44 _this.brightnessValue = v;
45 };
46
47 this.toHexString = function () {
48 return _this.color.toHexString();
49 };
50
51 this.toRgbString = function () {
52 return _this.color.toRgbString();
53 };
54
55 this.color = (0, _tinycolor2["default"])(input);
56
57 this.initRgb();
58 this.initHsb();
59
60 var initAlpha = input && input.alpha || this.color.toRgb().a;
61 this.alphaValue = Math.min(1, initAlpha) * 100;
62 }
63
64 Color.isValidHex = function isValidHex(hex) {
65 return (0, _tinycolor2["default"])(hex).isValid();
66 };
67
68 _createClass(Color, [{
69 key: 'hex',
70 get: function get() {
71 return this.color.toHex();
72 }
73
74 // 色调
75
76 }, {
77 key: 'hue',
78 set: function set(value) {
79 this.color = (0, _tinycolor2["default"])({
80 h: value,
81 s: this.saturation,
82 v: this.brightness
83 });
84
85 this.initRgb();
86 this.hueValue = value;
87 },
88 get: function get() {
89 return this.hueValue;
90 }
91
92 // 饱和度
93
94 }, {
95 key: 'saturation',
96 set: function set(value) {
97 this.color = (0, _tinycolor2["default"])({
98 h: this.hue,
99 s: value,
100 v: this.brightness
101 });
102
103 this.initRgb();
104 this.saturationValue = value;
105 },
106 get: function get() {
107 return this.saturationValue;
108 }
109
110 // 亮度
111
112 }, {
113 key: 'lightness',
114 set: function set(value) {
115 this.color = (0, _tinycolor2["default"])({
116 h: this.hue,
117 s: this.saturation,
118 l: value
119 });
120
121 this.initRgb();
122 this.lightnessValue = value;
123 },
124 get: function get() {
125 return this.lightnessValue;
126 }
127 }, {
128 key: 'brightness',
129 set: function set(value) {
130 this.color = (0, _tinycolor2["default"])({
131 h: this.hue,
132 s: this.saturation,
133 v: value
134 });
135
136 this.initRgb();
137 this.brightnessValue = value;
138 },
139 get: function get() {
140 return this.brightnessValue;
141 }
142
143 // red
144
145 }, {
146 key: 'red',
147 set: function set(value) {
148 var rgb = this.color.toRgb();
149 this.color = (0, _tinycolor2["default"])(_extends({}, rgb, {
150 r: value
151 }));
152
153 this.initHsb();
154 this.redValue = value;
155 },
156 get: function get() {
157 return this.redValue;
158 }
159
160 // green
161
162 }, {
163 key: 'green',
164 set: function set(value) {
165 var rgb = this.color.toRgb();
166 this.color = (0, _tinycolor2["default"])(_extends({}, rgb, {
167 g: value
168 }));
169
170 this.initHsb();
171 this.greenValue = value;
172 },
173 get: function get() {
174 return this.greenValue;
175 }
176
177 // blue
178
179 }, {
180 key: 'blue',
181 set: function set(value) {
182 var rgb = this.color.toRgb();
183 this.color = (0, _tinycolor2["default"])(_extends({}, rgb, {
184 b: value
185 }));
186
187 this.initHsb();
188 this.blueValue = value;
189 },
190 get: function get() {
191 return this.blueValue;
192 }
193
194 // alpha
195
196 }, {
197 key: 'alpha',
198 set: function set(value) {
199 this.color.setAlpha(value / 100);
200 },
201 get: function get() {
202 return this.color.getAlpha() * 100;
203 }
204 }, {
205 key: 'RGB',
206 get: function get() {
207 return [this.red, this.green, this.blue];
208 }
209 }, {
210 key: 'HSB',
211 get: function get() {
212 return [this.hue, this.saturation, this.brightness];
213 }
214 }]);
215
216 return Color;
217}();
218
219exports["default"] = Color;
220module.exports = exports['default'];
\No newline at end of file