@ant-design/fast-color
Advanced tools
Comparing version 1.0.0 to 1.1.0
@@ -48,2 +48,17 @@ import type { ColorInput, HSL, HSV, RGB } from './types'; | ||
lighten(amount?: number): FastColor; | ||
/** | ||
* Mix the color with pure white, from 0 to 100. | ||
* Providing 0 will do nothing, providing 100 will always return white. | ||
*/ | ||
tint(amount?: number): FastColor; | ||
/** | ||
* Mix the color with pure black, from 0 to 100. | ||
* Providing 0 will do nothing, providing 100 will always return black. | ||
*/ | ||
shade(amount?: number): FastColor; | ||
/** | ||
* Mix the current color a given amount with another color, from 0 to 100. | ||
* 0 means no mixing (return current color). | ||
*/ | ||
mix(color: ColorInput, amount?: number): FastColor; | ||
getAlpha(): number; | ||
@@ -66,4 +81,4 @@ /** | ||
getBrightness(): number; | ||
onBackground(bg: FastColor): FastColor; | ||
setAlpha(alpha: number): void; | ||
onBackground(background: ColorInput): FastColor; | ||
setAlpha(alpha: number): FastColor; | ||
toHexString(): string; | ||
@@ -70,0 +85,0 @@ toHsl(): HSL; |
@@ -1,7 +0,4 @@ | ||
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck"; | ||
import _createClass from "@babel/runtime/helpers/esm/createClass"; | ||
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty"; | ||
export var FastColor = /*#__PURE__*/function () { | ||
function FastColor(input) { | ||
_classCallCheck(this, FastColor); | ||
export class FastColor { | ||
constructor(input) { | ||
/** | ||
@@ -33,3 +30,3 @@ * Red, R in RGB | ||
if (typeof input === 'string') { | ||
var trimed = input.trim(); | ||
const trimed = input.trim(); | ||
if (trimed[0] === '#') { | ||
@@ -59,427 +56,398 @@ this.fromHexString(trimed); | ||
*/ | ||
_createClass(FastColor, [{ | ||
key: "h", | ||
get: function get() { | ||
return this.getHue(); | ||
} | ||
get h() { | ||
return this.getHue(); | ||
} | ||
/** | ||
* Saturation, S in HSL/HSV | ||
*/ | ||
}, { | ||
key: "s", | ||
get: function get() { | ||
return this.getSaturation(); | ||
} | ||
/** | ||
* Saturation, S in HSL/HSV | ||
*/ | ||
get s() { | ||
return this.getSaturation(); | ||
} | ||
/** | ||
* Lightness, L in HSL | ||
*/ | ||
}, { | ||
key: "l", | ||
get: function get() { | ||
return this.getLightness(); | ||
} | ||
/** | ||
* Lightness, L in HSL | ||
*/ | ||
get l() { | ||
return this.getLightness(); | ||
} | ||
/** | ||
* Value, V in HSV | ||
*/ | ||
}, { | ||
key: "v", | ||
get: function get() { | ||
return this.getValue(); | ||
/** | ||
* Value, V in HSV | ||
*/ | ||
get v() { | ||
return this.getValue(); | ||
} | ||
get isValid() { | ||
return typeof this.r === 'number' && this.r >= 0 && this.r <= 255 && typeof this.g === 'number' && this.g >= 0 && this.g <= 255 && typeof this.b === 'number' && this.b >= 0 && this.b <= 255 && typeof this.a === 'number' && this.a >= 0 && this.a <= 1; | ||
} | ||
clone() { | ||
return new FastColor(this); | ||
} | ||
equals(other) { | ||
return this.r === other.r && this.g === other.g && this.b === other.b && this.a === other.a; | ||
} | ||
darken(amount = 10) { | ||
const h = this.getHue(); | ||
const s = this.getSaturation(); | ||
let l = this.getLightness() - amount / 100; | ||
if (l < 0) { | ||
l = 0; | ||
} | ||
}, { | ||
key: "isValid", | ||
get: function get() { | ||
return typeof this.r === 'number' && this.r >= 0 && this.r <= 255 && typeof this.g === 'number' && this.g >= 0 && this.g <= 255 && typeof this.b === 'number' && this.b >= 0 && this.b <= 255 && typeof this.a === 'number' && this.a >= 0 && this.a <= 1; | ||
return new FastColor({ | ||
h, | ||
s, | ||
l, | ||
a: this.a | ||
}); | ||
} | ||
lighten(amount = 10) { | ||
const h = this.getHue(); | ||
const s = this.getSaturation(); | ||
let l = this.getLightness() + amount / 100; | ||
if (l > 1) { | ||
l = 1; | ||
} | ||
}, { | ||
key: "clone", | ||
value: function clone() { | ||
return new FastColor(this); | ||
return new FastColor({ | ||
h, | ||
s, | ||
l, | ||
a: this.a | ||
}); | ||
} | ||
/** | ||
* Mix the color with pure white, from 0 to 100. | ||
* Providing 0 will do nothing, providing 100 will always return white. | ||
*/ | ||
tint(amount = 10) { | ||
return this.mix({ | ||
r: 255, | ||
g: 255, | ||
b: 255, | ||
a: 1 | ||
}, amount); | ||
} | ||
/** | ||
* Mix the color with pure black, from 0 to 100. | ||
* Providing 0 will do nothing, providing 100 will always return black. | ||
*/ | ||
shade(amount = 10) { | ||
return this.mix({ | ||
r: 0, | ||
g: 0, | ||
b: 0, | ||
a: 1 | ||
}, amount); | ||
} | ||
/** | ||
* Mix the current color a given amount with another color, from 0 to 100. | ||
* 0 means no mixing (return current color). | ||
*/ | ||
mix(color, amount = 50) { | ||
const rgb1 = this.toRgb(); | ||
const rgb2 = new FastColor(color).toRgb(); | ||
const p = amount / 100; | ||
const rgba = { | ||
r: (rgb2.r - rgb1.r) * p + rgb1.r, | ||
g: (rgb2.g - rgb1.g) * p + rgb1.g, | ||
b: (rgb2.b - rgb1.b) * p + rgb1.b, | ||
a: (rgb2.a - rgb1.a) * p + rgb1.a | ||
}; | ||
return new FastColor(rgba); | ||
} | ||
getAlpha() { | ||
return this.a; | ||
} | ||
/** | ||
* Returns the perceived luminance of a color, from 0-1. | ||
* @see http://www.w3.org/TR/2008/REC-WCAG20-20081211/#relativeluminancedef | ||
*/ | ||
getLuminance() { | ||
let R; | ||
let G; | ||
let B; | ||
const RsRGB = this.r / 255; | ||
const GsRGB = this.g / 255; | ||
const BsRGB = this.b / 255; | ||
if (RsRGB <= 0.03928) { | ||
R = RsRGB / 12.92; | ||
} else { | ||
R = Math.pow((RsRGB + 0.055) / 1.055, 2.4); | ||
} | ||
}, { | ||
key: "equals", | ||
value: function equals(other) { | ||
return this.r === other.r && this.g === other.g && this.b === other.b && this.a === other.a; | ||
if (GsRGB <= 0.03928) { | ||
G = GsRGB / 12.92; | ||
} else { | ||
G = Math.pow((GsRGB + 0.055) / 1.055, 2.4); | ||
} | ||
}, { | ||
key: "darken", | ||
value: function darken() { | ||
var amount = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 10; | ||
var h = this.getHue(); | ||
var s = this.getSaturation(); | ||
var l = this.getLightness() - amount / 100; | ||
if (l < 0) { | ||
l = 0; | ||
} | ||
return new FastColor({ | ||
h: h, | ||
s: s, | ||
l: l, | ||
a: this.a | ||
}); | ||
if (BsRGB <= 0.03928) { | ||
B = BsRGB / 12.92; | ||
} else { | ||
B = Math.pow((BsRGB + 0.055) / 1.055, 2.4); | ||
} | ||
}, { | ||
key: "lighten", | ||
value: function lighten() { | ||
var amount = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 10; | ||
var h = this.getHue(); | ||
var s = this.getSaturation(); | ||
var l = this.getLightness() + amount / 100; | ||
if (l > 1) { | ||
l = 1; | ||
return 0.2126 * R + 0.7152 * G + 0.0722 * B; | ||
} | ||
getHue() { | ||
if (typeof this._h === 'undefined') { | ||
const delta = this.max - this.min; | ||
if (delta === 0) { | ||
this._h = 0; | ||
} else { | ||
this._h = Math.round(60 * (this.r === this.max ? (this.g - this.b) / delta + (this.g < this.b ? 6 : 0) : this.g === this.max ? (this.b - this.r) / delta + 2 : (this.r - this.g) / delta + 4)); | ||
} | ||
return new FastColor({ | ||
h: h, | ||
s: s, | ||
l: l, | ||
a: this.a | ||
}); | ||
} | ||
}, { | ||
key: "getAlpha", | ||
value: function getAlpha() { | ||
return this.a; | ||
} | ||
/** | ||
* Returns the perceived luminance of a color, from 0-1. | ||
* @see http://www.w3.org/TR/2008/REC-WCAG20-20081211/#relativeluminancedef | ||
*/ | ||
}, { | ||
key: "getLuminance", | ||
value: function getLuminance() { | ||
var R; | ||
var G; | ||
var B; | ||
var RsRGB = this.r / 255; | ||
var GsRGB = this.g / 255; | ||
var BsRGB = this.b / 255; | ||
if (RsRGB <= 0.03928) { | ||
R = RsRGB / 12.92; | ||
return this._h; | ||
} | ||
getSaturation() { | ||
if (typeof this._s === 'undefined') { | ||
const delta = this.max - this.min; | ||
if (delta === 0) { | ||
this._s = 0; | ||
} else { | ||
R = Math.pow((RsRGB + 0.055) / 1.055, 2.4); | ||
this._s = delta / this.max; | ||
} | ||
if (GsRGB <= 0.03928) { | ||
G = GsRGB / 12.92; | ||
} else { | ||
G = Math.pow((GsRGB + 0.055) / 1.055, 2.4); | ||
} | ||
if (BsRGB <= 0.03928) { | ||
B = BsRGB / 12.92; | ||
} else { | ||
B = Math.pow((BsRGB + 0.055) / 1.055, 2.4); | ||
} | ||
return 0.2126 * R + 0.7152 * G + 0.0722 * B; | ||
} | ||
}, { | ||
key: "getHue", | ||
value: function getHue() { | ||
if (typeof this._h === 'undefined') { | ||
var delta = this.max - this.min; | ||
if (delta === 0) { | ||
this._h = 0; | ||
} else { | ||
this._h = Math.round(60 * (this.r === this.max ? (this.g - this.b) / delta + (this.g < this.b ? 6 : 0) : this.g === this.max ? (this.b - this.r) / delta + 2 : (this.r - this.g) / delta + 4)); | ||
} | ||
} | ||
return this._h; | ||
return this._s; | ||
} | ||
getLightness() { | ||
if (typeof this._l === 'undefined') { | ||
this._l = (this.max + this.min) / 510; | ||
} | ||
}, { | ||
key: "getSaturation", | ||
value: function getSaturation() { | ||
if (typeof this._s === 'undefined') { | ||
var delta = this.max - this.min; | ||
if (delta === 0) { | ||
this._s = 0; | ||
} else { | ||
this._s = delta / this.max; | ||
} | ||
} | ||
return this._s; | ||
return this._l; | ||
} | ||
getValue() { | ||
if (typeof this._v === 'undefined') { | ||
this._v = this.max / 255; | ||
} | ||
}, { | ||
key: "getLightness", | ||
value: function getLightness() { | ||
if (typeof this._l === 'undefined') { | ||
this._l = (this.max + this.min) / 510; | ||
} | ||
return this._l; | ||
} | ||
}, { | ||
key: "getValue", | ||
value: function getValue() { | ||
if (typeof this._v === 'undefined') { | ||
this._v = this.max / 255; | ||
} | ||
return this._v; | ||
} | ||
}, { | ||
key: "isDark", | ||
value: function isDark() { | ||
return this.getBrightness() < 128; | ||
} | ||
}, { | ||
key: "isLight", | ||
value: function isLight() { | ||
return this.getBrightness() >= 128; | ||
} | ||
return this._v; | ||
} | ||
isDark() { | ||
return this.getBrightness() < 128; | ||
} | ||
isLight() { | ||
return this.getBrightness() >= 128; | ||
} | ||
/** | ||
* Returns the perceived brightness of the color, from 0-255. | ||
* @see http://www.w3.org/TR/AERT#color-contrast | ||
*/ | ||
}, { | ||
key: "getBrightness", | ||
value: function getBrightness() { | ||
if (typeof this._brightness === 'undefined') { | ||
this._brightness = (this.r * 299 + this.g * 587 + this.b * 114) / 1000; | ||
} | ||
return this._brightness; | ||
/** | ||
* Returns the perceived brightness of the color, from 0-255. | ||
* @see http://www.w3.org/TR/AERT#color-contrast | ||
*/ | ||
getBrightness() { | ||
if (typeof this._brightness === 'undefined') { | ||
this._brightness = (this.r * 299 + this.g * 587 + this.b * 114) / 1000; | ||
} | ||
}, { | ||
key: "onBackground", | ||
value: function onBackground(bg) { | ||
var alpha = this.a + bg.a * (1 - this.a); | ||
return new FastColor({ | ||
r: Math.round((this.r * this.a + bg.r * bg.a * (1 - this.a)) / alpha), | ||
g: Math.round((this.g * this.a + bg.g * bg.a * (1 - this.a)) / alpha), | ||
b: Math.round((this.b * this.a + bg.b * bg.a * (1 - this.a)) / alpha), | ||
a: alpha | ||
}); | ||
return this._brightness; | ||
} | ||
onBackground(background) { | ||
const bg = new FastColor(background); | ||
const alpha = this.a + bg.a * (1 - this.a); | ||
return new FastColor({ | ||
r: Math.round((this.r * this.a + bg.r * bg.a * (1 - this.a)) / alpha), | ||
g: Math.round((this.g * this.a + bg.g * bg.a * (1 - this.a)) / alpha), | ||
b: Math.round((this.b * this.a + bg.b * bg.a * (1 - this.a)) / alpha), | ||
a: alpha | ||
}); | ||
} | ||
setAlpha(alpha) { | ||
this.a = alpha; | ||
return this; | ||
} | ||
toHexString() { | ||
let hex = '#'; | ||
const rHex = (this.r || 0).toString(16); | ||
hex += rHex.length === 2 ? rHex : '0' + rHex; | ||
const gHex = (this.g || 0).toString(16); | ||
hex += gHex.length === 2 ? gHex : '0' + gHex; | ||
const bHex = (this.b || 0).toString(16); | ||
hex += bHex.length === 2 ? bHex : '0' + bHex; | ||
if (typeof this.a === 'number' && this.a >= 0 && this.a < 1) { | ||
const aHex = Math.round(this.a * 255).toString(16); | ||
hex += aHex.length === 2 ? aHex : '0' + aHex; | ||
} | ||
}, { | ||
key: "setAlpha", | ||
value: function setAlpha(alpha) { | ||
this.a = alpha; | ||
return hex; | ||
} | ||
toHsl() { | ||
return { | ||
h: this.h, | ||
s: this.s, | ||
l: this.l | ||
}; | ||
} | ||
toHslString() { | ||
const h = this.h; | ||
const s = Math.round(this.s * 100); | ||
const l = Math.round(this.l * 100); | ||
return this.a !== 1 ? `hsla(${h},${s}%,${l}%,${this.a})` : `hsl(${h},${s}%,${l}%)`; | ||
} | ||
toHsv() { | ||
return { | ||
h: this.h, | ||
s: this.s, | ||
v: this.v, | ||
a: this.a | ||
}; | ||
} | ||
toRgb() { | ||
return { | ||
r: this.r, | ||
g: this.g, | ||
b: this.b, | ||
a: this.a | ||
}; | ||
} | ||
toRgbString() { | ||
return this.a !== 1 ? `rgba(${this.r},${this.g},${this.b},${this.a})` : `rgb(${this.r},${this.g},${this.b})`; | ||
} | ||
toString() { | ||
return this.toRgbString(); | ||
} | ||
get max() { | ||
if (typeof this._max === 'undefined') { | ||
this._max = Math.max(this.r, this.g, this.b); | ||
} | ||
}, { | ||
key: "toHexString", | ||
value: function toHexString() { | ||
var hex = '#'; | ||
var rHex = (this.r || 0).toString(16); | ||
hex += rHex.length === 2 ? rHex : '0' + rHex; | ||
var gHex = (this.g || 0).toString(16); | ||
hex += gHex.length === 2 ? gHex : '0' + gHex; | ||
var bHex = (this.b || 0).toString(16); | ||
hex += bHex.length === 2 ? bHex : '0' + bHex; | ||
if (typeof this.a === 'number' && this.a >= 0 && this.a < 1) { | ||
var aHex = Math.round(this.a * 255).toString(16); | ||
hex += aHex.length === 2 ? aHex : '0' + aHex; | ||
} | ||
return hex; | ||
return this._max; | ||
} | ||
get min() { | ||
if (typeof this._min === 'undefined') { | ||
this._min = Math.min(this.r, this.g, this.b); | ||
} | ||
}, { | ||
key: "toHsl", | ||
value: function toHsl() { | ||
return { | ||
h: this.h, | ||
s: this.s, | ||
l: this.l | ||
}; | ||
return this._min; | ||
} | ||
fromHexString(trimed) { | ||
if (trimed.length < 6) { | ||
// #rgb or #rgba | ||
this.r = parseInt(trimed[1] + trimed[1], 16); | ||
this.g = parseInt(trimed[2] + trimed[2], 16); | ||
this.b = parseInt(trimed[3] + trimed[3], 16); | ||
this.a = trimed[4] ? parseInt(trimed[4] + trimed[4], 16) / 255 : 1; | ||
} else { | ||
// #rrggbb or #rrggbbaa | ||
this.r = parseInt(trimed[1] + trimed[2], 16); | ||
this.g = parseInt(trimed[3] + trimed[4], 16); | ||
this.b = parseInt(trimed[5] + trimed[6], 16); | ||
this.a = trimed[8] ? parseInt(trimed[7] + trimed[8], 16) / 255 : 1; | ||
} | ||
}, { | ||
key: "toHslString", | ||
value: function toHslString() { | ||
var h = this.h; | ||
var s = Math.round(this.s * 100); | ||
var l = Math.round(this.l * 100); | ||
return this.a !== 1 ? "hsla(".concat(h, ",").concat(s, "%,").concat(l, "%,").concat(this.a, ")") : "hsl(".concat(h, ",").concat(s, "%,").concat(l, "%)"); | ||
} | ||
fromHsl({ | ||
h, | ||
s, | ||
l, | ||
a | ||
}) { | ||
this._h = h % 360; | ||
this._s = s; | ||
this._l = l; | ||
this.a = typeof a === 'number' ? a : 1; | ||
if (s <= 0) { | ||
const rgb = Math.round(l * 255); | ||
this.r = rgb; | ||
this.g = rgb; | ||
this.b = rgb; | ||
} | ||
}, { | ||
key: "toHsv", | ||
value: function toHsv() { | ||
return { | ||
h: this.h, | ||
s: this.s, | ||
v: this.v, | ||
a: this.a | ||
}; | ||
const huePrime = h / 60; | ||
const chroma = (1 - Math.abs(2 * l - 1)) * s; | ||
const secondComponent = chroma * (1 - Math.abs(huePrime % 2 - 1)); | ||
this.r = 0; | ||
this.g = 0; | ||
this.b = 0; | ||
if (huePrime >= 0 && huePrime < 1) { | ||
this.r = chroma; | ||
this.g = secondComponent; | ||
} else if (huePrime >= 1 && huePrime < 2) { | ||
this.r = secondComponent; | ||
this.g = chroma; | ||
} else if (huePrime >= 2 && huePrime < 3) { | ||
this.g = chroma; | ||
this.b = secondComponent; | ||
} else if (huePrime >= 3 && huePrime < 4) { | ||
this.g = secondComponent; | ||
this.b = chroma; | ||
} else if (huePrime >= 4 && huePrime < 5) { | ||
this.r = secondComponent; | ||
this.b = chroma; | ||
} else if (huePrime >= 5 && huePrime < 6) { | ||
this.r = chroma; | ||
this.b = secondComponent; | ||
} | ||
}, { | ||
key: "toRgb", | ||
value: function toRgb() { | ||
return { | ||
r: this.r, | ||
g: this.g, | ||
b: this.b, | ||
a: this.a | ||
}; | ||
const lightnessModification = l - chroma / 2; | ||
this.r = Math.round((this.r + lightnessModification) * 255); | ||
this.g = Math.round((this.g + lightnessModification) * 255); | ||
this.b = Math.round((this.b + lightnessModification) * 255); | ||
} | ||
fromHsv({ | ||
h, | ||
s, | ||
v, | ||
a | ||
}) { | ||
this._h = h % 360; | ||
this._s = s; | ||
this._v = v; | ||
this.a = typeof a === 'number' ? a : 1; | ||
const vv = Math.round(v * 255); | ||
this.r = vv; | ||
this.g = vv; | ||
this.b = vv; | ||
if (s <= 0) { | ||
return; | ||
} | ||
}, { | ||
key: "toRgbString", | ||
value: function toRgbString() { | ||
return this.a !== 1 ? "rgba(".concat(this.r, ",").concat(this.g, ",").concat(this.b, ",").concat(this.a, ")") : "rgb(".concat(this.r, ",").concat(this.g, ",").concat(this.b, ")"); | ||
const hh = h / 60; | ||
const i = Math.floor(hh); | ||
const ff = hh - i; | ||
const p = Math.round(v * (1.0 - s) * 255); | ||
const q = Math.round(v * (1.0 - s * ff) * 255); | ||
const t = Math.round(v * (1.0 - s * (1.0 - ff)) * 255); | ||
switch (i) { | ||
case 0: | ||
this.g = t; | ||
this.b = p; | ||
break; | ||
case 1: | ||
this.r = q; | ||
this.b = p; | ||
break; | ||
case 2: | ||
this.r = p; | ||
this.b = t; | ||
break; | ||
case 3: | ||
this.r = p; | ||
this.g = q; | ||
break; | ||
case 4: | ||
this.r = t; | ||
this.g = p; | ||
break; | ||
case 5: | ||
default: | ||
this.g = p; | ||
this.b = q; | ||
break; | ||
} | ||
}, { | ||
key: "toString", | ||
value: function toString() { | ||
return this.toRgbString(); | ||
} | ||
}, { | ||
key: "max", | ||
get: function get() { | ||
if (typeof this._max === 'undefined') { | ||
this._max = Math.max(this.r, this.g, this.b); | ||
} | ||
return this._max; | ||
} | ||
}, { | ||
key: "min", | ||
get: function get() { | ||
if (typeof this._min === 'undefined') { | ||
this._min = Math.min(this.r, this.g, this.b); | ||
} | ||
return this._min; | ||
} | ||
}, { | ||
key: "fromHexString", | ||
value: function fromHexString(trimed) { | ||
if (trimed.length < 6) { | ||
// #rgb or #rgba | ||
this.r = parseInt(trimed[1] + trimed[1], 16); | ||
this.g = parseInt(trimed[2] + trimed[2], 16); | ||
this.b = parseInt(trimed[3] + trimed[3], 16); | ||
this.a = trimed[4] ? parseInt(trimed[4] + trimed[4], 16) / 255 : 1; | ||
} else { | ||
// #rrggbb or #rrggbbaa | ||
this.r = parseInt(trimed[1] + trimed[2], 16); | ||
this.g = parseInt(trimed[3] + trimed[4], 16); | ||
this.b = parseInt(trimed[5] + trimed[6], 16); | ||
this.a = trimed[8] ? parseInt(trimed[7] + trimed[8], 16) / 255 : 1; | ||
} | ||
} | ||
}, { | ||
key: "fromHsl", | ||
value: function fromHsl(_ref) { | ||
var h = _ref.h, | ||
s = _ref.s, | ||
l = _ref.l, | ||
a = _ref.a; | ||
this._h = h % 360; | ||
this._s = s; | ||
this._l = l; | ||
this.a = typeof a === 'number' ? a : 1; | ||
if (s <= 0) { | ||
var rgb = Math.round(l * 255); | ||
this.r = rgb; | ||
this.g = rgb; | ||
this.b = rgb; | ||
} | ||
var huePrime = h / 60; | ||
var chroma = (1 - Math.abs(2 * l - 1)) * s; | ||
var secondComponent = chroma * (1 - Math.abs(huePrime % 2 - 1)); | ||
this.r = 0; | ||
this.g = 0; | ||
this.b = 0; | ||
if (huePrime >= 0 && huePrime < 1) { | ||
this.r = chroma; | ||
this.g = secondComponent; | ||
} else if (huePrime >= 1 && huePrime < 2) { | ||
this.r = secondComponent; | ||
this.g = chroma; | ||
} else if (huePrime >= 2 && huePrime < 3) { | ||
this.g = chroma; | ||
this.b = secondComponent; | ||
} else if (huePrime >= 3 && huePrime < 4) { | ||
this.g = secondComponent; | ||
this.b = chroma; | ||
} else if (huePrime >= 4 && huePrime < 5) { | ||
this.r = secondComponent; | ||
this.b = chroma; | ||
} else if (huePrime >= 5 && huePrime < 6) { | ||
this.r = chroma; | ||
this.b = secondComponent; | ||
} | ||
var lightnessModification = l - chroma / 2; | ||
this.r = Math.round((this.r + lightnessModification) * 255); | ||
this.g = Math.round((this.g + lightnessModification) * 255); | ||
this.b = Math.round((this.b + lightnessModification) * 255); | ||
} | ||
}, { | ||
key: "fromHsv", | ||
value: function fromHsv(_ref2) { | ||
var h = _ref2.h, | ||
s = _ref2.s, | ||
v = _ref2.v, | ||
a = _ref2.a; | ||
this._h = h % 360; | ||
this._s = s; | ||
this._v = v; | ||
this.a = typeof a === 'number' ? a : 1; | ||
var vv = Math.round(v * 255); | ||
this.r = vv; | ||
this.g = vv; | ||
this.b = vv; | ||
if (s <= 0) { | ||
return; | ||
} | ||
var hh = h / 60; | ||
var i = Math.floor(hh); | ||
var ff = hh - i; | ||
var p = Math.round(v * (1.0 - s) * 255); | ||
var q = Math.round(v * (1.0 - s * ff) * 255); | ||
var t = Math.round(v * (1.0 - s * (1.0 - ff)) * 255); | ||
switch (i) { | ||
case 0: | ||
this.g = t; | ||
this.b = p; | ||
break; | ||
case 1: | ||
this.r = q; | ||
this.b = p; | ||
break; | ||
case 2: | ||
this.r = p; | ||
this.b = t; | ||
break; | ||
case 3: | ||
this.r = p; | ||
this.g = q; | ||
break; | ||
case 4: | ||
this.r = t; | ||
this.g = p; | ||
break; | ||
case 5: | ||
default: | ||
this.g = p; | ||
this.b = q; | ||
break; | ||
} | ||
} | ||
}, { | ||
key: "fromHslString", | ||
value: function fromHslString(trimed) { | ||
var str = trimed.substring(trimed.indexOf('(') + 1, trimed.indexOf(')')); | ||
var arr = str.includes(',') ? str.split(',') : str.replace('/', ' ').split(' ').filter(function (item) { | ||
return item.length > 0; | ||
}); | ||
var h = parseInt(arr[0]); | ||
var s = parseFloat(arr[1]) / 100; | ||
var l = parseFloat(arr[2]) / 100; | ||
var a = arr[3] ? arr[3].includes('%') ? parseFloat(arr[3]) / 100 : parseFloat(arr[3]) : 1; | ||
this.fromHsl({ | ||
h: h, | ||
s: s, | ||
l: l, | ||
a: a | ||
}); | ||
} | ||
}, { | ||
key: "fromRgbString", | ||
value: function fromRgbString(trimed) { | ||
var str = trimed.substring(trimed.indexOf('(') + 1, trimed.indexOf(')')); | ||
var arr = str.includes(',') ? str.split(',') : str.replace('/', ' ').split(' ').filter(function (item) { | ||
return item.length > 0; | ||
}); | ||
this.r = arr[0].includes('%') ? Math.round(parseFloat(arr[0]) / 100 * 255) : parseInt(arr[0]); | ||
this.g = arr[1].includes('%') ? Math.round(parseFloat(arr[1]) / 100 * 255) : parseInt(arr[1]); | ||
this.b = arr[2].includes('%') ? Math.round(parseFloat(arr[2]) / 100 * 255) : parseInt(arr[2]); | ||
this.a = arr[3] ? arr[3].includes('%') ? parseFloat(arr[3]) / 100 : parseFloat(arr[3]) : 1; | ||
} | ||
}]); | ||
return FastColor; | ||
}(); | ||
} | ||
fromHslString(trimed) { | ||
const str = trimed.substring(trimed.indexOf('(') + 1, trimed.indexOf(')')); | ||
const arr = str.includes(',') ? str.split(',') : str.replace('/', ' ').split(' ').filter(item => item.length > 0); | ||
const h = parseInt(arr[0]); | ||
const s = parseFloat(arr[1]) / 100; | ||
const l = parseFloat(arr[2]) / 100; | ||
const a = arr[3] ? arr[3].includes('%') ? parseFloat(arr[3]) / 100 : parseFloat(arr[3]) : 1; | ||
this.fromHsl({ | ||
h, | ||
s, | ||
l, | ||
a | ||
}); | ||
} | ||
fromRgbString(trimed) { | ||
const str = trimed.substring(trimed.indexOf('(') + 1, trimed.indexOf(')')); | ||
const arr = str.includes(',') ? str.split(',') : str.replace('/', ' ').split(' ').filter(item => item.length > 0); | ||
this.r = arr[0].includes('%') ? Math.round(parseFloat(arr[0]) / 100 * 255) : parseInt(arr[0]); | ||
this.g = arr[1].includes('%') ? Math.round(parseFloat(arr[1]) / 100 * 255) : parseInt(arr[1]); | ||
this.b = arr[2].includes('%') ? Math.round(parseFloat(arr[2]) / 100 * 255) : parseInt(arr[2]); | ||
this.a = arr[3] ? arr[3].includes('%') ? parseFloat(arr[3]) / 100 : parseFloat(arr[3]) : 1; | ||
} | ||
} |
@@ -48,2 +48,17 @@ import type { ColorInput, HSL, HSV, RGB } from './types'; | ||
lighten(amount?: number): FastColor; | ||
/** | ||
* Mix the color with pure white, from 0 to 100. | ||
* Providing 0 will do nothing, providing 100 will always return white. | ||
*/ | ||
tint(amount?: number): FastColor; | ||
/** | ||
* Mix the color with pure black, from 0 to 100. | ||
* Providing 0 will do nothing, providing 100 will always return black. | ||
*/ | ||
shade(amount?: number): FastColor; | ||
/** | ||
* Mix the current color a given amount with another color, from 0 to 100. | ||
* 0 means no mixing (return current color). | ||
*/ | ||
mix(color: ColorInput, amount?: number): FastColor; | ||
getAlpha(): number; | ||
@@ -66,4 +81,4 @@ /** | ||
getBrightness(): number; | ||
onBackground(bg: FastColor): FastColor; | ||
setAlpha(alpha: number): void; | ||
onBackground(background: ColorInput): FastColor; | ||
setAlpha(alpha: number): FastColor; | ||
toHexString(): string; | ||
@@ -70,0 +85,0 @@ toHsl(): HSL; |
@@ -8,8 +8,5 @@ "use strict"; | ||
exports.FastColor = void 0; | ||
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck")); | ||
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass")); | ||
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty")); | ||
var FastColor = exports.FastColor = /*#__PURE__*/function () { | ||
function FastColor(input) { | ||
(0, _classCallCheck2.default)(this, FastColor); | ||
class FastColor { | ||
constructor(input) { | ||
/** | ||
@@ -41,3 +38,3 @@ * Red, R in RGB | ||
if (typeof input === 'string') { | ||
var trimed = input.trim(); | ||
const trimed = input.trim(); | ||
if (trimed[0] === '#') { | ||
@@ -67,427 +64,399 @@ this.fromHexString(trimed); | ||
*/ | ||
(0, _createClass2.default)(FastColor, [{ | ||
key: "h", | ||
get: function get() { | ||
return this.getHue(); | ||
} | ||
get h() { | ||
return this.getHue(); | ||
} | ||
/** | ||
* Saturation, S in HSL/HSV | ||
*/ | ||
}, { | ||
key: "s", | ||
get: function get() { | ||
return this.getSaturation(); | ||
} | ||
/** | ||
* Saturation, S in HSL/HSV | ||
*/ | ||
get s() { | ||
return this.getSaturation(); | ||
} | ||
/** | ||
* Lightness, L in HSL | ||
*/ | ||
}, { | ||
key: "l", | ||
get: function get() { | ||
return this.getLightness(); | ||
} | ||
/** | ||
* Lightness, L in HSL | ||
*/ | ||
get l() { | ||
return this.getLightness(); | ||
} | ||
/** | ||
* Value, V in HSV | ||
*/ | ||
}, { | ||
key: "v", | ||
get: function get() { | ||
return this.getValue(); | ||
/** | ||
* Value, V in HSV | ||
*/ | ||
get v() { | ||
return this.getValue(); | ||
} | ||
get isValid() { | ||
return typeof this.r === 'number' && this.r >= 0 && this.r <= 255 && typeof this.g === 'number' && this.g >= 0 && this.g <= 255 && typeof this.b === 'number' && this.b >= 0 && this.b <= 255 && typeof this.a === 'number' && this.a >= 0 && this.a <= 1; | ||
} | ||
clone() { | ||
return new FastColor(this); | ||
} | ||
equals(other) { | ||
return this.r === other.r && this.g === other.g && this.b === other.b && this.a === other.a; | ||
} | ||
darken(amount = 10) { | ||
const h = this.getHue(); | ||
const s = this.getSaturation(); | ||
let l = this.getLightness() - amount / 100; | ||
if (l < 0) { | ||
l = 0; | ||
} | ||
}, { | ||
key: "isValid", | ||
get: function get() { | ||
return typeof this.r === 'number' && this.r >= 0 && this.r <= 255 && typeof this.g === 'number' && this.g >= 0 && this.g <= 255 && typeof this.b === 'number' && this.b >= 0 && this.b <= 255 && typeof this.a === 'number' && this.a >= 0 && this.a <= 1; | ||
return new FastColor({ | ||
h, | ||
s, | ||
l, | ||
a: this.a | ||
}); | ||
} | ||
lighten(amount = 10) { | ||
const h = this.getHue(); | ||
const s = this.getSaturation(); | ||
let l = this.getLightness() + amount / 100; | ||
if (l > 1) { | ||
l = 1; | ||
} | ||
}, { | ||
key: "clone", | ||
value: function clone() { | ||
return new FastColor(this); | ||
return new FastColor({ | ||
h, | ||
s, | ||
l, | ||
a: this.a | ||
}); | ||
} | ||
/** | ||
* Mix the color with pure white, from 0 to 100. | ||
* Providing 0 will do nothing, providing 100 will always return white. | ||
*/ | ||
tint(amount = 10) { | ||
return this.mix({ | ||
r: 255, | ||
g: 255, | ||
b: 255, | ||
a: 1 | ||
}, amount); | ||
} | ||
/** | ||
* Mix the color with pure black, from 0 to 100. | ||
* Providing 0 will do nothing, providing 100 will always return black. | ||
*/ | ||
shade(amount = 10) { | ||
return this.mix({ | ||
r: 0, | ||
g: 0, | ||
b: 0, | ||
a: 1 | ||
}, amount); | ||
} | ||
/** | ||
* Mix the current color a given amount with another color, from 0 to 100. | ||
* 0 means no mixing (return current color). | ||
*/ | ||
mix(color, amount = 50) { | ||
const rgb1 = this.toRgb(); | ||
const rgb2 = new FastColor(color).toRgb(); | ||
const p = amount / 100; | ||
const rgba = { | ||
r: (rgb2.r - rgb1.r) * p + rgb1.r, | ||
g: (rgb2.g - rgb1.g) * p + rgb1.g, | ||
b: (rgb2.b - rgb1.b) * p + rgb1.b, | ||
a: (rgb2.a - rgb1.a) * p + rgb1.a | ||
}; | ||
return new FastColor(rgba); | ||
} | ||
getAlpha() { | ||
return this.a; | ||
} | ||
/** | ||
* Returns the perceived luminance of a color, from 0-1. | ||
* @see http://www.w3.org/TR/2008/REC-WCAG20-20081211/#relativeluminancedef | ||
*/ | ||
getLuminance() { | ||
let R; | ||
let G; | ||
let B; | ||
const RsRGB = this.r / 255; | ||
const GsRGB = this.g / 255; | ||
const BsRGB = this.b / 255; | ||
if (RsRGB <= 0.03928) { | ||
R = RsRGB / 12.92; | ||
} else { | ||
R = Math.pow((RsRGB + 0.055) / 1.055, 2.4); | ||
} | ||
}, { | ||
key: "equals", | ||
value: function equals(other) { | ||
return this.r === other.r && this.g === other.g && this.b === other.b && this.a === other.a; | ||
if (GsRGB <= 0.03928) { | ||
G = GsRGB / 12.92; | ||
} else { | ||
G = Math.pow((GsRGB + 0.055) / 1.055, 2.4); | ||
} | ||
}, { | ||
key: "darken", | ||
value: function darken() { | ||
var amount = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 10; | ||
var h = this.getHue(); | ||
var s = this.getSaturation(); | ||
var l = this.getLightness() - amount / 100; | ||
if (l < 0) { | ||
l = 0; | ||
} | ||
return new FastColor({ | ||
h: h, | ||
s: s, | ||
l: l, | ||
a: this.a | ||
}); | ||
if (BsRGB <= 0.03928) { | ||
B = BsRGB / 12.92; | ||
} else { | ||
B = Math.pow((BsRGB + 0.055) / 1.055, 2.4); | ||
} | ||
}, { | ||
key: "lighten", | ||
value: function lighten() { | ||
var amount = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 10; | ||
var h = this.getHue(); | ||
var s = this.getSaturation(); | ||
var l = this.getLightness() + amount / 100; | ||
if (l > 1) { | ||
l = 1; | ||
return 0.2126 * R + 0.7152 * G + 0.0722 * B; | ||
} | ||
getHue() { | ||
if (typeof this._h === 'undefined') { | ||
const delta = this.max - this.min; | ||
if (delta === 0) { | ||
this._h = 0; | ||
} else { | ||
this._h = Math.round(60 * (this.r === this.max ? (this.g - this.b) / delta + (this.g < this.b ? 6 : 0) : this.g === this.max ? (this.b - this.r) / delta + 2 : (this.r - this.g) / delta + 4)); | ||
} | ||
return new FastColor({ | ||
h: h, | ||
s: s, | ||
l: l, | ||
a: this.a | ||
}); | ||
} | ||
}, { | ||
key: "getAlpha", | ||
value: function getAlpha() { | ||
return this.a; | ||
} | ||
/** | ||
* Returns the perceived luminance of a color, from 0-1. | ||
* @see http://www.w3.org/TR/2008/REC-WCAG20-20081211/#relativeluminancedef | ||
*/ | ||
}, { | ||
key: "getLuminance", | ||
value: function getLuminance() { | ||
var R; | ||
var G; | ||
var B; | ||
var RsRGB = this.r / 255; | ||
var GsRGB = this.g / 255; | ||
var BsRGB = this.b / 255; | ||
if (RsRGB <= 0.03928) { | ||
R = RsRGB / 12.92; | ||
return this._h; | ||
} | ||
getSaturation() { | ||
if (typeof this._s === 'undefined') { | ||
const delta = this.max - this.min; | ||
if (delta === 0) { | ||
this._s = 0; | ||
} else { | ||
R = Math.pow((RsRGB + 0.055) / 1.055, 2.4); | ||
this._s = delta / this.max; | ||
} | ||
if (GsRGB <= 0.03928) { | ||
G = GsRGB / 12.92; | ||
} else { | ||
G = Math.pow((GsRGB + 0.055) / 1.055, 2.4); | ||
} | ||
if (BsRGB <= 0.03928) { | ||
B = BsRGB / 12.92; | ||
} else { | ||
B = Math.pow((BsRGB + 0.055) / 1.055, 2.4); | ||
} | ||
return 0.2126 * R + 0.7152 * G + 0.0722 * B; | ||
} | ||
}, { | ||
key: "getHue", | ||
value: function getHue() { | ||
if (typeof this._h === 'undefined') { | ||
var delta = this.max - this.min; | ||
if (delta === 0) { | ||
this._h = 0; | ||
} else { | ||
this._h = Math.round(60 * (this.r === this.max ? (this.g - this.b) / delta + (this.g < this.b ? 6 : 0) : this.g === this.max ? (this.b - this.r) / delta + 2 : (this.r - this.g) / delta + 4)); | ||
} | ||
} | ||
return this._h; | ||
return this._s; | ||
} | ||
getLightness() { | ||
if (typeof this._l === 'undefined') { | ||
this._l = (this.max + this.min) / 510; | ||
} | ||
}, { | ||
key: "getSaturation", | ||
value: function getSaturation() { | ||
if (typeof this._s === 'undefined') { | ||
var delta = this.max - this.min; | ||
if (delta === 0) { | ||
this._s = 0; | ||
} else { | ||
this._s = delta / this.max; | ||
} | ||
} | ||
return this._s; | ||
return this._l; | ||
} | ||
getValue() { | ||
if (typeof this._v === 'undefined') { | ||
this._v = this.max / 255; | ||
} | ||
}, { | ||
key: "getLightness", | ||
value: function getLightness() { | ||
if (typeof this._l === 'undefined') { | ||
this._l = (this.max + this.min) / 510; | ||
} | ||
return this._l; | ||
} | ||
}, { | ||
key: "getValue", | ||
value: function getValue() { | ||
if (typeof this._v === 'undefined') { | ||
this._v = this.max / 255; | ||
} | ||
return this._v; | ||
} | ||
}, { | ||
key: "isDark", | ||
value: function isDark() { | ||
return this.getBrightness() < 128; | ||
} | ||
}, { | ||
key: "isLight", | ||
value: function isLight() { | ||
return this.getBrightness() >= 128; | ||
} | ||
return this._v; | ||
} | ||
isDark() { | ||
return this.getBrightness() < 128; | ||
} | ||
isLight() { | ||
return this.getBrightness() >= 128; | ||
} | ||
/** | ||
* Returns the perceived brightness of the color, from 0-255. | ||
* @see http://www.w3.org/TR/AERT#color-contrast | ||
*/ | ||
}, { | ||
key: "getBrightness", | ||
value: function getBrightness() { | ||
if (typeof this._brightness === 'undefined') { | ||
this._brightness = (this.r * 299 + this.g * 587 + this.b * 114) / 1000; | ||
} | ||
return this._brightness; | ||
/** | ||
* Returns the perceived brightness of the color, from 0-255. | ||
* @see http://www.w3.org/TR/AERT#color-contrast | ||
*/ | ||
getBrightness() { | ||
if (typeof this._brightness === 'undefined') { | ||
this._brightness = (this.r * 299 + this.g * 587 + this.b * 114) / 1000; | ||
} | ||
}, { | ||
key: "onBackground", | ||
value: function onBackground(bg) { | ||
var alpha = this.a + bg.a * (1 - this.a); | ||
return new FastColor({ | ||
r: Math.round((this.r * this.a + bg.r * bg.a * (1 - this.a)) / alpha), | ||
g: Math.round((this.g * this.a + bg.g * bg.a * (1 - this.a)) / alpha), | ||
b: Math.round((this.b * this.a + bg.b * bg.a * (1 - this.a)) / alpha), | ||
a: alpha | ||
}); | ||
return this._brightness; | ||
} | ||
onBackground(background) { | ||
const bg = new FastColor(background); | ||
const alpha = this.a + bg.a * (1 - this.a); | ||
return new FastColor({ | ||
r: Math.round((this.r * this.a + bg.r * bg.a * (1 - this.a)) / alpha), | ||
g: Math.round((this.g * this.a + bg.g * bg.a * (1 - this.a)) / alpha), | ||
b: Math.round((this.b * this.a + bg.b * bg.a * (1 - this.a)) / alpha), | ||
a: alpha | ||
}); | ||
} | ||
setAlpha(alpha) { | ||
this.a = alpha; | ||
return this; | ||
} | ||
toHexString() { | ||
let hex = '#'; | ||
const rHex = (this.r || 0).toString(16); | ||
hex += rHex.length === 2 ? rHex : '0' + rHex; | ||
const gHex = (this.g || 0).toString(16); | ||
hex += gHex.length === 2 ? gHex : '0' + gHex; | ||
const bHex = (this.b || 0).toString(16); | ||
hex += bHex.length === 2 ? bHex : '0' + bHex; | ||
if (typeof this.a === 'number' && this.a >= 0 && this.a < 1) { | ||
const aHex = Math.round(this.a * 255).toString(16); | ||
hex += aHex.length === 2 ? aHex : '0' + aHex; | ||
} | ||
}, { | ||
key: "setAlpha", | ||
value: function setAlpha(alpha) { | ||
this.a = alpha; | ||
return hex; | ||
} | ||
toHsl() { | ||
return { | ||
h: this.h, | ||
s: this.s, | ||
l: this.l | ||
}; | ||
} | ||
toHslString() { | ||
const h = this.h; | ||
const s = Math.round(this.s * 100); | ||
const l = Math.round(this.l * 100); | ||
return this.a !== 1 ? `hsla(${h},${s}%,${l}%,${this.a})` : `hsl(${h},${s}%,${l}%)`; | ||
} | ||
toHsv() { | ||
return { | ||
h: this.h, | ||
s: this.s, | ||
v: this.v, | ||
a: this.a | ||
}; | ||
} | ||
toRgb() { | ||
return { | ||
r: this.r, | ||
g: this.g, | ||
b: this.b, | ||
a: this.a | ||
}; | ||
} | ||
toRgbString() { | ||
return this.a !== 1 ? `rgba(${this.r},${this.g},${this.b},${this.a})` : `rgb(${this.r},${this.g},${this.b})`; | ||
} | ||
toString() { | ||
return this.toRgbString(); | ||
} | ||
get max() { | ||
if (typeof this._max === 'undefined') { | ||
this._max = Math.max(this.r, this.g, this.b); | ||
} | ||
}, { | ||
key: "toHexString", | ||
value: function toHexString() { | ||
var hex = '#'; | ||
var rHex = (this.r || 0).toString(16); | ||
hex += rHex.length === 2 ? rHex : '0' + rHex; | ||
var gHex = (this.g || 0).toString(16); | ||
hex += gHex.length === 2 ? gHex : '0' + gHex; | ||
var bHex = (this.b || 0).toString(16); | ||
hex += bHex.length === 2 ? bHex : '0' + bHex; | ||
if (typeof this.a === 'number' && this.a >= 0 && this.a < 1) { | ||
var aHex = Math.round(this.a * 255).toString(16); | ||
hex += aHex.length === 2 ? aHex : '0' + aHex; | ||
} | ||
return hex; | ||
return this._max; | ||
} | ||
get min() { | ||
if (typeof this._min === 'undefined') { | ||
this._min = Math.min(this.r, this.g, this.b); | ||
} | ||
}, { | ||
key: "toHsl", | ||
value: function toHsl() { | ||
return { | ||
h: this.h, | ||
s: this.s, | ||
l: this.l | ||
}; | ||
return this._min; | ||
} | ||
fromHexString(trimed) { | ||
if (trimed.length < 6) { | ||
// #rgb or #rgba | ||
this.r = parseInt(trimed[1] + trimed[1], 16); | ||
this.g = parseInt(trimed[2] + trimed[2], 16); | ||
this.b = parseInt(trimed[3] + trimed[3], 16); | ||
this.a = trimed[4] ? parseInt(trimed[4] + trimed[4], 16) / 255 : 1; | ||
} else { | ||
// #rrggbb or #rrggbbaa | ||
this.r = parseInt(trimed[1] + trimed[2], 16); | ||
this.g = parseInt(trimed[3] + trimed[4], 16); | ||
this.b = parseInt(trimed[5] + trimed[6], 16); | ||
this.a = trimed[8] ? parseInt(trimed[7] + trimed[8], 16) / 255 : 1; | ||
} | ||
}, { | ||
key: "toHslString", | ||
value: function toHslString() { | ||
var h = this.h; | ||
var s = Math.round(this.s * 100); | ||
var l = Math.round(this.l * 100); | ||
return this.a !== 1 ? "hsla(".concat(h, ",").concat(s, "%,").concat(l, "%,").concat(this.a, ")") : "hsl(".concat(h, ",").concat(s, "%,").concat(l, "%)"); | ||
} | ||
fromHsl({ | ||
h, | ||
s, | ||
l, | ||
a | ||
}) { | ||
this._h = h % 360; | ||
this._s = s; | ||
this._l = l; | ||
this.a = typeof a === 'number' ? a : 1; | ||
if (s <= 0) { | ||
const rgb = Math.round(l * 255); | ||
this.r = rgb; | ||
this.g = rgb; | ||
this.b = rgb; | ||
} | ||
}, { | ||
key: "toHsv", | ||
value: function toHsv() { | ||
return { | ||
h: this.h, | ||
s: this.s, | ||
v: this.v, | ||
a: this.a | ||
}; | ||
const huePrime = h / 60; | ||
const chroma = (1 - Math.abs(2 * l - 1)) * s; | ||
const secondComponent = chroma * (1 - Math.abs(huePrime % 2 - 1)); | ||
this.r = 0; | ||
this.g = 0; | ||
this.b = 0; | ||
if (huePrime >= 0 && huePrime < 1) { | ||
this.r = chroma; | ||
this.g = secondComponent; | ||
} else if (huePrime >= 1 && huePrime < 2) { | ||
this.r = secondComponent; | ||
this.g = chroma; | ||
} else if (huePrime >= 2 && huePrime < 3) { | ||
this.g = chroma; | ||
this.b = secondComponent; | ||
} else if (huePrime >= 3 && huePrime < 4) { | ||
this.g = secondComponent; | ||
this.b = chroma; | ||
} else if (huePrime >= 4 && huePrime < 5) { | ||
this.r = secondComponent; | ||
this.b = chroma; | ||
} else if (huePrime >= 5 && huePrime < 6) { | ||
this.r = chroma; | ||
this.b = secondComponent; | ||
} | ||
}, { | ||
key: "toRgb", | ||
value: function toRgb() { | ||
return { | ||
r: this.r, | ||
g: this.g, | ||
b: this.b, | ||
a: this.a | ||
}; | ||
const lightnessModification = l - chroma / 2; | ||
this.r = Math.round((this.r + lightnessModification) * 255); | ||
this.g = Math.round((this.g + lightnessModification) * 255); | ||
this.b = Math.round((this.b + lightnessModification) * 255); | ||
} | ||
fromHsv({ | ||
h, | ||
s, | ||
v, | ||
a | ||
}) { | ||
this._h = h % 360; | ||
this._s = s; | ||
this._v = v; | ||
this.a = typeof a === 'number' ? a : 1; | ||
const vv = Math.round(v * 255); | ||
this.r = vv; | ||
this.g = vv; | ||
this.b = vv; | ||
if (s <= 0) { | ||
return; | ||
} | ||
}, { | ||
key: "toRgbString", | ||
value: function toRgbString() { | ||
return this.a !== 1 ? "rgba(".concat(this.r, ",").concat(this.g, ",").concat(this.b, ",").concat(this.a, ")") : "rgb(".concat(this.r, ",").concat(this.g, ",").concat(this.b, ")"); | ||
const hh = h / 60; | ||
const i = Math.floor(hh); | ||
const ff = hh - i; | ||
const p = Math.round(v * (1.0 - s) * 255); | ||
const q = Math.round(v * (1.0 - s * ff) * 255); | ||
const t = Math.round(v * (1.0 - s * (1.0 - ff)) * 255); | ||
switch (i) { | ||
case 0: | ||
this.g = t; | ||
this.b = p; | ||
break; | ||
case 1: | ||
this.r = q; | ||
this.b = p; | ||
break; | ||
case 2: | ||
this.r = p; | ||
this.b = t; | ||
break; | ||
case 3: | ||
this.r = p; | ||
this.g = q; | ||
break; | ||
case 4: | ||
this.r = t; | ||
this.g = p; | ||
break; | ||
case 5: | ||
default: | ||
this.g = p; | ||
this.b = q; | ||
break; | ||
} | ||
}, { | ||
key: "toString", | ||
value: function toString() { | ||
return this.toRgbString(); | ||
} | ||
}, { | ||
key: "max", | ||
get: function get() { | ||
if (typeof this._max === 'undefined') { | ||
this._max = Math.max(this.r, this.g, this.b); | ||
} | ||
return this._max; | ||
} | ||
}, { | ||
key: "min", | ||
get: function get() { | ||
if (typeof this._min === 'undefined') { | ||
this._min = Math.min(this.r, this.g, this.b); | ||
} | ||
return this._min; | ||
} | ||
}, { | ||
key: "fromHexString", | ||
value: function fromHexString(trimed) { | ||
if (trimed.length < 6) { | ||
// #rgb or #rgba | ||
this.r = parseInt(trimed[1] + trimed[1], 16); | ||
this.g = parseInt(trimed[2] + trimed[2], 16); | ||
this.b = parseInt(trimed[3] + trimed[3], 16); | ||
this.a = trimed[4] ? parseInt(trimed[4] + trimed[4], 16) / 255 : 1; | ||
} else { | ||
// #rrggbb or #rrggbbaa | ||
this.r = parseInt(trimed[1] + trimed[2], 16); | ||
this.g = parseInt(trimed[3] + trimed[4], 16); | ||
this.b = parseInt(trimed[5] + trimed[6], 16); | ||
this.a = trimed[8] ? parseInt(trimed[7] + trimed[8], 16) / 255 : 1; | ||
} | ||
} | ||
}, { | ||
key: "fromHsl", | ||
value: function fromHsl(_ref) { | ||
var h = _ref.h, | ||
s = _ref.s, | ||
l = _ref.l, | ||
a = _ref.a; | ||
this._h = h % 360; | ||
this._s = s; | ||
this._l = l; | ||
this.a = typeof a === 'number' ? a : 1; | ||
if (s <= 0) { | ||
var rgb = Math.round(l * 255); | ||
this.r = rgb; | ||
this.g = rgb; | ||
this.b = rgb; | ||
} | ||
var huePrime = h / 60; | ||
var chroma = (1 - Math.abs(2 * l - 1)) * s; | ||
var secondComponent = chroma * (1 - Math.abs(huePrime % 2 - 1)); | ||
this.r = 0; | ||
this.g = 0; | ||
this.b = 0; | ||
if (huePrime >= 0 && huePrime < 1) { | ||
this.r = chroma; | ||
this.g = secondComponent; | ||
} else if (huePrime >= 1 && huePrime < 2) { | ||
this.r = secondComponent; | ||
this.g = chroma; | ||
} else if (huePrime >= 2 && huePrime < 3) { | ||
this.g = chroma; | ||
this.b = secondComponent; | ||
} else if (huePrime >= 3 && huePrime < 4) { | ||
this.g = secondComponent; | ||
this.b = chroma; | ||
} else if (huePrime >= 4 && huePrime < 5) { | ||
this.r = secondComponent; | ||
this.b = chroma; | ||
} else if (huePrime >= 5 && huePrime < 6) { | ||
this.r = chroma; | ||
this.b = secondComponent; | ||
} | ||
var lightnessModification = l - chroma / 2; | ||
this.r = Math.round((this.r + lightnessModification) * 255); | ||
this.g = Math.round((this.g + lightnessModification) * 255); | ||
this.b = Math.round((this.b + lightnessModification) * 255); | ||
} | ||
}, { | ||
key: "fromHsv", | ||
value: function fromHsv(_ref2) { | ||
var h = _ref2.h, | ||
s = _ref2.s, | ||
v = _ref2.v, | ||
a = _ref2.a; | ||
this._h = h % 360; | ||
this._s = s; | ||
this._v = v; | ||
this.a = typeof a === 'number' ? a : 1; | ||
var vv = Math.round(v * 255); | ||
this.r = vv; | ||
this.g = vv; | ||
this.b = vv; | ||
if (s <= 0) { | ||
return; | ||
} | ||
var hh = h / 60; | ||
var i = Math.floor(hh); | ||
var ff = hh - i; | ||
var p = Math.round(v * (1.0 - s) * 255); | ||
var q = Math.round(v * (1.0 - s * ff) * 255); | ||
var t = Math.round(v * (1.0 - s * (1.0 - ff)) * 255); | ||
switch (i) { | ||
case 0: | ||
this.g = t; | ||
this.b = p; | ||
break; | ||
case 1: | ||
this.r = q; | ||
this.b = p; | ||
break; | ||
case 2: | ||
this.r = p; | ||
this.b = t; | ||
break; | ||
case 3: | ||
this.r = p; | ||
this.g = q; | ||
break; | ||
case 4: | ||
this.r = t; | ||
this.g = p; | ||
break; | ||
case 5: | ||
default: | ||
this.g = p; | ||
this.b = q; | ||
break; | ||
} | ||
} | ||
}, { | ||
key: "fromHslString", | ||
value: function fromHslString(trimed) { | ||
var str = trimed.substring(trimed.indexOf('(') + 1, trimed.indexOf(')')); | ||
var arr = str.includes(',') ? str.split(',') : str.replace('/', ' ').split(' ').filter(function (item) { | ||
return item.length > 0; | ||
}); | ||
var h = parseInt(arr[0]); | ||
var s = parseFloat(arr[1]) / 100; | ||
var l = parseFloat(arr[2]) / 100; | ||
var a = arr[3] ? arr[3].includes('%') ? parseFloat(arr[3]) / 100 : parseFloat(arr[3]) : 1; | ||
this.fromHsl({ | ||
h: h, | ||
s: s, | ||
l: l, | ||
a: a | ||
}); | ||
} | ||
}, { | ||
key: "fromRgbString", | ||
value: function fromRgbString(trimed) { | ||
var str = trimed.substring(trimed.indexOf('(') + 1, trimed.indexOf(')')); | ||
var arr = str.includes(',') ? str.split(',') : str.replace('/', ' ').split(' ').filter(function (item) { | ||
return item.length > 0; | ||
}); | ||
this.r = arr[0].includes('%') ? Math.round(parseFloat(arr[0]) / 100 * 255) : parseInt(arr[0]); | ||
this.g = arr[1].includes('%') ? Math.round(parseFloat(arr[1]) / 100 * 255) : parseInt(arr[1]); | ||
this.b = arr[2].includes('%') ? Math.round(parseFloat(arr[2]) / 100 * 255) : parseInt(arr[2]); | ||
this.a = arr[3] ? arr[3].includes('%') ? parseFloat(arr[3]) / 100 : parseFloat(arr[3]) : 1; | ||
} | ||
}]); | ||
return FastColor; | ||
}(); | ||
} | ||
fromHslString(trimed) { | ||
const str = trimed.substring(trimed.indexOf('(') + 1, trimed.indexOf(')')); | ||
const arr = str.includes(',') ? str.split(',') : str.replace('/', ' ').split(' ').filter(item => item.length > 0); | ||
const h = parseInt(arr[0]); | ||
const s = parseFloat(arr[1]) / 100; | ||
const l = parseFloat(arr[2]) / 100; | ||
const a = arr[3] ? arr[3].includes('%') ? parseFloat(arr[3]) / 100 : parseFloat(arr[3]) : 1; | ||
this.fromHsl({ | ||
h, | ||
s, | ||
l, | ||
a | ||
}); | ||
} | ||
fromRgbString(trimed) { | ||
const str = trimed.substring(trimed.indexOf('(') + 1, trimed.indexOf(')')); | ||
const arr = str.includes(',') ? str.split(',') : str.replace('/', ' ').split(' ').filter(item => item.length > 0); | ||
this.r = arr[0].includes('%') ? Math.round(parseFloat(arr[0]) / 100 * 255) : parseInt(arr[0]); | ||
this.g = arr[1].includes('%') ? Math.round(parseFloat(arr[1]) / 100 * 255) : parseInt(arr[1]); | ||
this.b = arr[2].includes('%') ? Math.round(parseFloat(arr[2]) / 100 * 255) : parseInt(arr[2]); | ||
this.a = arr[3] ? arr[3].includes('%') ? parseFloat(arr[3]) / 100 : parseFloat(arr[3]) : 1; | ||
} | ||
} | ||
exports.FastColor = FastColor; |
@@ -12,3 +12,3 @@ "use strict"; | ||
enumerable: true, | ||
get: function get() { | ||
get: function () { | ||
return _FastColor[key]; | ||
@@ -24,3 +24,3 @@ } | ||
enumerable: true, | ||
get: function get() { | ||
get: function () { | ||
return _types[key]; | ||
@@ -27,0 +27,0 @@ } |
{ | ||
"name": "@ant-design/fast-color", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "fast and small color class", | ||
@@ -5,0 +5,0 @@ "engines": { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
39461
1152