@ant-design/fast-color
Advanced tools
Comparing version 1.2.6 to 1.3.0
import type { ColorInput, HSL, HSV, RGB } from './types'; | ||
export declare class FastColor { | ||
/** | ||
* All FastColor objects are valid. So isValid is always true. This property is kept to be compatible with TinyColor. | ||
*/ | ||
isValid: true; | ||
/** | ||
* Red, R in RGB | ||
@@ -27,19 +31,7 @@ */ | ||
constructor(input: ColorInput); | ||
/** | ||
* Hue, H in HSL/HSV | ||
*/ | ||
get h(): number; | ||
/** | ||
* Saturation, S in HSL/HSV | ||
*/ | ||
get s(): number; | ||
/** | ||
* Lightness, L in HSL | ||
*/ | ||
get l(): number; | ||
/** | ||
* Value, V in HSV | ||
*/ | ||
get v(): number; | ||
get isValid(): boolean; | ||
setR(value: number): this; | ||
setG(value: number): this; | ||
setB(value: number): this; | ||
getAlpha(): number; | ||
setAlpha(value: number): this; | ||
clone(): FastColor; | ||
@@ -63,4 +55,3 @@ equals(other: FastColor): boolean; | ||
*/ | ||
mix(color: ColorInput, amount?: number): FastColor; | ||
getAlpha(): number; | ||
mix(input: ColorInput, amount?: number): FastColor; | ||
/** | ||
@@ -83,3 +74,2 @@ * Returns the perceived luminance of a color, from 0-1. | ||
onBackground(background: ColorInput): FastColor; | ||
setAlpha(alpha: number): FastColor; | ||
toHexString(): string; | ||
@@ -92,4 +82,4 @@ toHsl(): HSL; | ||
toString(): string; | ||
private get max(); | ||
private get min(); | ||
private getMax; | ||
private getMin; | ||
private fromHexString; | ||
@@ -96,0 +86,0 @@ private fromHsl; |
@@ -1,2 +0,1 @@ | ||
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty"; | ||
/** | ||
@@ -32,38 +31,62 @@ * Support format, alpha unit will check the % mark: | ||
} | ||
function inRange(val, max = 255, min = 0) { | ||
return typeof val === 'number' && val >= min && val <= max; | ||
const parseHSVorHSL = (num, _, index) => index === 0 ? num : num / 100; | ||
/** round and limit number to integer between 0-255 */ | ||
function limitRange(value, max) { | ||
const mergedMax = max || 255; | ||
if (value > mergedMax) { | ||
return mergedMax; | ||
} | ||
if (value < 0) { | ||
return 0; | ||
} | ||
return value; | ||
} | ||
const parseHSVorHSL = (num, _, index) => index === 0 ? num : num / 100; | ||
export class FastColor { | ||
/** | ||
* All FastColor objects are valid. So isValid is always true. This property is kept to be compatible with TinyColor. | ||
*/ | ||
isValid; | ||
/** | ||
* Red, R in RGB | ||
*/ | ||
r = 0; | ||
/** | ||
* Green, G in RGB | ||
*/ | ||
g = 0; | ||
/** | ||
* Blue, B in RGB | ||
*/ | ||
b = 0; | ||
/** | ||
* Alpha/Opacity, A in RGBA/HSLA | ||
*/ | ||
a = 1; | ||
// HSV privates | ||
_h; | ||
_s; | ||
_l; | ||
_v; | ||
// intermedia variables to calculate HSL/HSV | ||
_max; | ||
_min; | ||
_brightness; | ||
constructor(input) { | ||
/** | ||
* Red, R in RGB | ||
* Always check 3 char in the object to determine the format. | ||
* We not use function in check to save bundle size. | ||
* e.g. 'rgb' -> { r: 0, g: 0, b: 0 }. | ||
*/ | ||
_defineProperty(this, "r", void 0); | ||
/** | ||
* Green, G in RGB | ||
*/ | ||
_defineProperty(this, "g", void 0); | ||
/** | ||
* Blue, B in RGB | ||
*/ | ||
_defineProperty(this, "b", void 0); | ||
/** | ||
* Alpha/Opacity, A in RGBA/HSLA | ||
*/ | ||
_defineProperty(this, "a", void 0); | ||
// HSV privates | ||
_defineProperty(this, "_h", void 0); | ||
_defineProperty(this, "_s", void 0); | ||
_defineProperty(this, "_l", void 0); | ||
_defineProperty(this, "_v", void 0); | ||
// intermedia variables to calculate HSL/HSV | ||
_defineProperty(this, "_max", void 0); | ||
_defineProperty(this, "_min", void 0); | ||
_defineProperty(this, "_brightness", void 0); | ||
function matchFormat(str) { | ||
return str[0] in input && str[1] in input && str[2] in input; | ||
} | ||
if (!input) { | ||
this.r = 0; | ||
this.g = 0; | ||
this.b = 0; | ||
this.a = 1; | ||
// Do nothing since already initialized | ||
} else if (typeof input === 'string') { | ||
@@ -83,10 +106,10 @@ const trimStr = input.trim(); | ||
} | ||
} else if ('r' in input && 'g' in input && 'b' in input) { | ||
this.r = input.r; | ||
this.g = input.g; | ||
this.b = input.b; | ||
this.a = typeof input.a === 'number' ? input.a : 1; | ||
} else if ('l' in input && 'h' in input && 's' in input) { | ||
} else if (matchFormat('rgb')) { | ||
this.setR(input.r); | ||
this.setG(input.g); | ||
this.setB(input.b); | ||
this.setAlpha(typeof input.a === 'number' ? input.a : 1); | ||
} else if (matchFormat('hsl')) { | ||
this.fromHsl(input); | ||
} else if ('v' in input && 'h' in input && 's' in input) { | ||
} else if (matchFormat('hsv')) { | ||
this.fromHsv(input); | ||
@@ -97,32 +120,20 @@ } else { | ||
} | ||
/** | ||
* Hue, H in HSL/HSV | ||
*/ | ||
get h() { | ||
return this.getHue(); | ||
setR(value) { | ||
this.r = limitRange(value); | ||
return this; | ||
} | ||
/** | ||
* Saturation, S in HSL/HSV | ||
*/ | ||
get s() { | ||
return this.getSaturation(); | ||
setG(value) { | ||
this.g = limitRange(value); | ||
return this; | ||
} | ||
/** | ||
* Lightness, L in HSL | ||
*/ | ||
get l() { | ||
return this.getLightness(); | ||
setB(value) { | ||
this.b = limitRange(value); | ||
return this; | ||
} | ||
/** | ||
* Value, V in HSV | ||
*/ | ||
get v() { | ||
return this.getValue(); | ||
getAlpha() { | ||
return this.a; | ||
} | ||
get isValid() { | ||
return inRange(this.r) && inRange(this.g) && inRange(this.b) && inRange(this.a, 1); | ||
setAlpha(value) { | ||
this.a = limitRange(value, 1); | ||
return this; | ||
} | ||
@@ -194,17 +205,13 @@ clone() { | ||
*/ | ||
mix(color, amount = 50) { | ||
const rgb1 = this.toRgb(); | ||
const rgb2 = new FastColor(color).toRgb(); | ||
mix(input, amount = 50) { | ||
const color = new FastColor(input); | ||
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 | ||
r: (color.r - this.r) * p + this.r, | ||
g: (color.g - this.g) * p + this.g, | ||
b: (color.b - this.b) * p + this.b, | ||
a: (color.a - this.a) * p + this.a | ||
}; | ||
return new FastColor(rgba); | ||
} | ||
getAlpha() { | ||
return this.a; | ||
} | ||
@@ -216,23 +223,9 @@ /** | ||
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); | ||
function adjustGamma(raw) { | ||
const val = raw / 255; | ||
return val <= 0.03928 ? val / 12.92 : Math.pow((val + 0.055) / 1.055, 2.4); | ||
} | ||
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); | ||
} | ||
const R = adjustGamma(this.r); | ||
const G = adjustGamma(this.g); | ||
const B = adjustGamma(this.b); | ||
return 0.2126 * R + 0.7152 * G + 0.0722 * B; | ||
@@ -242,7 +235,7 @@ } | ||
if (typeof this._h === 'undefined') { | ||
const delta = this.max - this.min; | ||
const delta = this.getMax() - this.getMin(); | ||
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)); | ||
this._h = Math.round(60 * (this.r === this.getMax() ? (this.g - this.b) / delta + (this.g < this.b ? 6 : 0) : this.g === this.getMax() ? (this.b - this.r) / delta + 2 : (this.r - this.g) / delta + 4)); | ||
} | ||
@@ -254,7 +247,7 @@ } | ||
if (typeof this._s === 'undefined') { | ||
const delta = this.max - this.min; | ||
const delta = this.getMax() - this.getMin(); | ||
if (delta === 0) { | ||
this._s = 0; | ||
} else { | ||
this._s = delta / this.max; | ||
this._s = delta / this.getMax(); | ||
} | ||
@@ -266,3 +259,3 @@ } | ||
if (typeof this._l === 'undefined') { | ||
this._l = (this.max + this.min) / 510; | ||
this._l = (this.getMax() + this.getMin()) / 510; | ||
} | ||
@@ -273,3 +266,3 @@ return this._l; | ||
if (typeof this._v === 'undefined') { | ||
this._v = this.max / 255; | ||
this._v = this.getMax() / 255; | ||
} | ||
@@ -305,6 +298,2 @@ return this._v; | ||
} | ||
setAlpha(alpha) { | ||
this.a = alpha; | ||
return this; | ||
} | ||
toHexString() { | ||
@@ -326,12 +315,12 @@ let hex = '#'; | ||
return { | ||
h: this.h, | ||
s: this.s, | ||
l: this.l, | ||
a: this.a | ||
h: this.getHue(), | ||
s: this.getSaturation(), | ||
l: this.getLightness(), | ||
a: this.getAlpha() | ||
}; | ||
} | ||
toHslString() { | ||
const h = this.h; | ||
const s = Math.round(this.s * 100); | ||
const l = Math.round(this.l * 100); | ||
const h = this.getHue(); | ||
const s = Math.round(this.getSaturation() * 100); | ||
const l = Math.round(this.getLightness() * 100); | ||
return this.a !== 1 ? `hsla(${h},${s}%,${l}%,${this.a})` : `hsl(${h},${s}%,${l}%)`; | ||
@@ -341,6 +330,6 @@ } | ||
return { | ||
h: this.h, | ||
s: this.s, | ||
v: this.v, | ||
a: this.a | ||
h: this.getHue(), | ||
s: this.getSaturation(), | ||
v: this.getValue(), | ||
a: this.getAlpha() | ||
}; | ||
@@ -362,3 +351,3 @@ } | ||
} | ||
get max() { | ||
getMax() { | ||
if (typeof this._max === 'undefined') { | ||
@@ -369,3 +358,3 @@ this._max = Math.max(this.r, this.g, this.b); | ||
} | ||
get min() { | ||
getMin() { | ||
if (typeof this._min === 'undefined') { | ||
@@ -411,31 +400,31 @@ this._min = Math.min(this.r, this.g, this.b); | ||
} | ||
let r = 0, | ||
g = 0, | ||
b = 0; | ||
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; | ||
r = chroma; | ||
g = secondComponent; | ||
} else if (huePrime >= 1 && huePrime < 2) { | ||
this.r = secondComponent; | ||
this.g = chroma; | ||
r = secondComponent; | ||
g = chroma; | ||
} else if (huePrime >= 2 && huePrime < 3) { | ||
this.g = chroma; | ||
this.b = secondComponent; | ||
g = chroma; | ||
b = secondComponent; | ||
} else if (huePrime >= 3 && huePrime < 4) { | ||
this.g = secondComponent; | ||
this.b = chroma; | ||
g = secondComponent; | ||
b = chroma; | ||
} else if (huePrime >= 4 && huePrime < 5) { | ||
this.r = secondComponent; | ||
this.b = chroma; | ||
r = secondComponent; | ||
b = chroma; | ||
} else if (huePrime >= 5 && huePrime < 6) { | ||
this.r = chroma; | ||
this.b = secondComponent; | ||
r = chroma; | ||
b = secondComponent; | ||
} | ||
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); | ||
this.r = Math.round((r + lightnessModification) * 255); | ||
this.g = Math.round((g + lightnessModification) * 255); | ||
this.b = Math.round((b + lightnessModification) * 255); | ||
} | ||
@@ -442,0 +431,0 @@ fromHsv({ |
import type { ColorInput, HSL, HSV, RGB } from './types'; | ||
export declare class FastColor { | ||
/** | ||
* All FastColor objects are valid. So isValid is always true. This property is kept to be compatible with TinyColor. | ||
*/ | ||
isValid: true; | ||
/** | ||
* Red, R in RGB | ||
@@ -27,19 +31,7 @@ */ | ||
constructor(input: ColorInput); | ||
/** | ||
* Hue, H in HSL/HSV | ||
*/ | ||
get h(): number; | ||
/** | ||
* Saturation, S in HSL/HSV | ||
*/ | ||
get s(): number; | ||
/** | ||
* Lightness, L in HSL | ||
*/ | ||
get l(): number; | ||
/** | ||
* Value, V in HSV | ||
*/ | ||
get v(): number; | ||
get isValid(): boolean; | ||
setR(value: number): this; | ||
setG(value: number): this; | ||
setB(value: number): this; | ||
getAlpha(): number; | ||
setAlpha(value: number): this; | ||
clone(): FastColor; | ||
@@ -63,4 +55,3 @@ equals(other: FastColor): boolean; | ||
*/ | ||
mix(color: ColorInput, amount?: number): FastColor; | ||
getAlpha(): number; | ||
mix(input: ColorInput, amount?: number): FastColor; | ||
/** | ||
@@ -83,3 +74,2 @@ * Returns the perceived luminance of a color, from 0-1. | ||
onBackground(background: ColorInput): FastColor; | ||
setAlpha(alpha: number): FastColor; | ||
toHexString(): string; | ||
@@ -92,4 +82,4 @@ toHsl(): HSL; | ||
toString(): string; | ||
private get max(); | ||
private get min(); | ||
private getMax; | ||
private getMin; | ||
private fromHexString; | ||
@@ -96,0 +86,0 @@ private fromHsl; |
"use strict"; | ||
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default; | ||
Object.defineProperty(exports, "__esModule", { | ||
@@ -8,3 +7,2 @@ value: true | ||
exports.FastColor = void 0; | ||
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty")); | ||
/** | ||
@@ -40,38 +38,62 @@ * Support format, alpha unit will check the % mark: | ||
} | ||
function inRange(val, max = 255, min = 0) { | ||
return typeof val === 'number' && val >= min && val <= max; | ||
const parseHSVorHSL = (num, _, index) => index === 0 ? num : num / 100; | ||
/** round and limit number to integer between 0-255 */ | ||
function limitRange(value, max) { | ||
const mergedMax = max || 255; | ||
if (value > mergedMax) { | ||
return mergedMax; | ||
} | ||
if (value < 0) { | ||
return 0; | ||
} | ||
return value; | ||
} | ||
const parseHSVorHSL = (num, _, index) => index === 0 ? num : num / 100; | ||
class FastColor { | ||
/** | ||
* All FastColor objects are valid. So isValid is always true. This property is kept to be compatible with TinyColor. | ||
*/ | ||
isValid; | ||
/** | ||
* Red, R in RGB | ||
*/ | ||
r = 0; | ||
/** | ||
* Green, G in RGB | ||
*/ | ||
g = 0; | ||
/** | ||
* Blue, B in RGB | ||
*/ | ||
b = 0; | ||
/** | ||
* Alpha/Opacity, A in RGBA/HSLA | ||
*/ | ||
a = 1; | ||
// HSV privates | ||
_h; | ||
_s; | ||
_l; | ||
_v; | ||
// intermedia variables to calculate HSL/HSV | ||
_max; | ||
_min; | ||
_brightness; | ||
constructor(input) { | ||
/** | ||
* Red, R in RGB | ||
* Always check 3 char in the object to determine the format. | ||
* We not use function in check to save bundle size. | ||
* e.g. 'rgb' -> { r: 0, g: 0, b: 0 }. | ||
*/ | ||
(0, _defineProperty2.default)(this, "r", void 0); | ||
/** | ||
* Green, G in RGB | ||
*/ | ||
(0, _defineProperty2.default)(this, "g", void 0); | ||
/** | ||
* Blue, B in RGB | ||
*/ | ||
(0, _defineProperty2.default)(this, "b", void 0); | ||
/** | ||
* Alpha/Opacity, A in RGBA/HSLA | ||
*/ | ||
(0, _defineProperty2.default)(this, "a", void 0); | ||
// HSV privates | ||
(0, _defineProperty2.default)(this, "_h", void 0); | ||
(0, _defineProperty2.default)(this, "_s", void 0); | ||
(0, _defineProperty2.default)(this, "_l", void 0); | ||
(0, _defineProperty2.default)(this, "_v", void 0); | ||
// intermedia variables to calculate HSL/HSV | ||
(0, _defineProperty2.default)(this, "_max", void 0); | ||
(0, _defineProperty2.default)(this, "_min", void 0); | ||
(0, _defineProperty2.default)(this, "_brightness", void 0); | ||
function matchFormat(str) { | ||
return str[0] in input && str[1] in input && str[2] in input; | ||
} | ||
if (!input) { | ||
this.r = 0; | ||
this.g = 0; | ||
this.b = 0; | ||
this.a = 1; | ||
// Do nothing since already initialized | ||
} else if (typeof input === 'string') { | ||
@@ -91,10 +113,10 @@ const trimStr = input.trim(); | ||
} | ||
} else if ('r' in input && 'g' in input && 'b' in input) { | ||
this.r = input.r; | ||
this.g = input.g; | ||
this.b = input.b; | ||
this.a = typeof input.a === 'number' ? input.a : 1; | ||
} else if ('l' in input && 'h' in input && 's' in input) { | ||
} else if (matchFormat('rgb')) { | ||
this.setR(input.r); | ||
this.setG(input.g); | ||
this.setB(input.b); | ||
this.setAlpha(typeof input.a === 'number' ? input.a : 1); | ||
} else if (matchFormat('hsl')) { | ||
this.fromHsl(input); | ||
} else if ('v' in input && 'h' in input && 's' in input) { | ||
} else if (matchFormat('hsv')) { | ||
this.fromHsv(input); | ||
@@ -105,32 +127,20 @@ } else { | ||
} | ||
/** | ||
* Hue, H in HSL/HSV | ||
*/ | ||
get h() { | ||
return this.getHue(); | ||
setR(value) { | ||
this.r = limitRange(value); | ||
return this; | ||
} | ||
/** | ||
* Saturation, S in HSL/HSV | ||
*/ | ||
get s() { | ||
return this.getSaturation(); | ||
setG(value) { | ||
this.g = limitRange(value); | ||
return this; | ||
} | ||
/** | ||
* Lightness, L in HSL | ||
*/ | ||
get l() { | ||
return this.getLightness(); | ||
setB(value) { | ||
this.b = limitRange(value); | ||
return this; | ||
} | ||
/** | ||
* Value, V in HSV | ||
*/ | ||
get v() { | ||
return this.getValue(); | ||
getAlpha() { | ||
return this.a; | ||
} | ||
get isValid() { | ||
return inRange(this.r) && inRange(this.g) && inRange(this.b) && inRange(this.a, 1); | ||
setAlpha(value) { | ||
this.a = limitRange(value, 1); | ||
return this; | ||
} | ||
@@ -202,17 +212,13 @@ clone() { | ||
*/ | ||
mix(color, amount = 50) { | ||
const rgb1 = this.toRgb(); | ||
const rgb2 = new FastColor(color).toRgb(); | ||
mix(input, amount = 50) { | ||
const color = new FastColor(input); | ||
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 | ||
r: (color.r - this.r) * p + this.r, | ||
g: (color.g - this.g) * p + this.g, | ||
b: (color.b - this.b) * p + this.b, | ||
a: (color.a - this.a) * p + this.a | ||
}; | ||
return new FastColor(rgba); | ||
} | ||
getAlpha() { | ||
return this.a; | ||
} | ||
@@ -224,23 +230,9 @@ /** | ||
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); | ||
function adjustGamma(raw) { | ||
const val = raw / 255; | ||
return val <= 0.03928 ? val / 12.92 : Math.pow((val + 0.055) / 1.055, 2.4); | ||
} | ||
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); | ||
} | ||
const R = adjustGamma(this.r); | ||
const G = adjustGamma(this.g); | ||
const B = adjustGamma(this.b); | ||
return 0.2126 * R + 0.7152 * G + 0.0722 * B; | ||
@@ -250,7 +242,7 @@ } | ||
if (typeof this._h === 'undefined') { | ||
const delta = this.max - this.min; | ||
const delta = this.getMax() - this.getMin(); | ||
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)); | ||
this._h = Math.round(60 * (this.r === this.getMax() ? (this.g - this.b) / delta + (this.g < this.b ? 6 : 0) : this.g === this.getMax() ? (this.b - this.r) / delta + 2 : (this.r - this.g) / delta + 4)); | ||
} | ||
@@ -262,7 +254,7 @@ } | ||
if (typeof this._s === 'undefined') { | ||
const delta = this.max - this.min; | ||
const delta = this.getMax() - this.getMin(); | ||
if (delta === 0) { | ||
this._s = 0; | ||
} else { | ||
this._s = delta / this.max; | ||
this._s = delta / this.getMax(); | ||
} | ||
@@ -274,3 +266,3 @@ } | ||
if (typeof this._l === 'undefined') { | ||
this._l = (this.max + this.min) / 510; | ||
this._l = (this.getMax() + this.getMin()) / 510; | ||
} | ||
@@ -281,3 +273,3 @@ return this._l; | ||
if (typeof this._v === 'undefined') { | ||
this._v = this.max / 255; | ||
this._v = this.getMax() / 255; | ||
} | ||
@@ -313,6 +305,2 @@ return this._v; | ||
} | ||
setAlpha(alpha) { | ||
this.a = alpha; | ||
return this; | ||
} | ||
toHexString() { | ||
@@ -334,12 +322,12 @@ let hex = '#'; | ||
return { | ||
h: this.h, | ||
s: this.s, | ||
l: this.l, | ||
a: this.a | ||
h: this.getHue(), | ||
s: this.getSaturation(), | ||
l: this.getLightness(), | ||
a: this.getAlpha() | ||
}; | ||
} | ||
toHslString() { | ||
const h = this.h; | ||
const s = Math.round(this.s * 100); | ||
const l = Math.round(this.l * 100); | ||
const h = this.getHue(); | ||
const s = Math.round(this.getSaturation() * 100); | ||
const l = Math.round(this.getLightness() * 100); | ||
return this.a !== 1 ? `hsla(${h},${s}%,${l}%,${this.a})` : `hsl(${h},${s}%,${l}%)`; | ||
@@ -349,6 +337,6 @@ } | ||
return { | ||
h: this.h, | ||
s: this.s, | ||
v: this.v, | ||
a: this.a | ||
h: this.getHue(), | ||
s: this.getSaturation(), | ||
v: this.getValue(), | ||
a: this.getAlpha() | ||
}; | ||
@@ -370,3 +358,3 @@ } | ||
} | ||
get max() { | ||
getMax() { | ||
if (typeof this._max === 'undefined') { | ||
@@ -377,3 +365,3 @@ this._max = Math.max(this.r, this.g, this.b); | ||
} | ||
get min() { | ||
getMin() { | ||
if (typeof this._min === 'undefined') { | ||
@@ -419,31 +407,31 @@ this._min = Math.min(this.r, this.g, this.b); | ||
} | ||
let r = 0, | ||
g = 0, | ||
b = 0; | ||
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; | ||
r = chroma; | ||
g = secondComponent; | ||
} else if (huePrime >= 1 && huePrime < 2) { | ||
this.r = secondComponent; | ||
this.g = chroma; | ||
r = secondComponent; | ||
g = chroma; | ||
} else if (huePrime >= 2 && huePrime < 3) { | ||
this.g = chroma; | ||
this.b = secondComponent; | ||
g = chroma; | ||
b = secondComponent; | ||
} else if (huePrime >= 3 && huePrime < 4) { | ||
this.g = secondComponent; | ||
this.b = chroma; | ||
g = secondComponent; | ||
b = chroma; | ||
} else if (huePrime >= 4 && huePrime < 5) { | ||
this.r = secondComponent; | ||
this.b = chroma; | ||
r = secondComponent; | ||
b = chroma; | ||
} else if (huePrime >= 5 && huePrime < 6) { | ||
this.r = chroma; | ||
this.b = secondComponent; | ||
r = chroma; | ||
b = secondComponent; | ||
} | ||
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); | ||
this.r = Math.round((r + lightnessModification) * 255); | ||
this.g = Math.round((g + lightnessModification) * 255); | ||
this.b = Math.round((b + lightnessModification) * 255); | ||
} | ||
@@ -450,0 +438,0 @@ fromHsv({ |
{ | ||
"name": "@ant-design/fast-color", | ||
"version": "1.2.6", | ||
"version": "1.3.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
40407
1221