Socket
Socket
Sign inDemoInstall

@pixi/color

Package Overview
Dependencies
Maintainers
2
Versions
20
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@pixi/color - npm Package Compare versions

Comparing version 7.2.4 to 7.3.0-rc

1

lib/Color.d.ts

@@ -226,2 +226,3 @@ import type { HslaColor, HslColor, HsvaColor, HsvColor, RgbaColor, RgbColor } from 'colord/types';

* @param steps - Number of steps which will be used as a cap when rounding colors
* @deprecated since 7.3.0
*/

@@ -228,0 +229,0 @@ round(steps: number): this;

334

lib/Color.js

@@ -1,47 +0,59 @@

'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
var colord = require('colord');
var namesPlugin = require('colord/plugins/names');
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
var namesPlugin__default = /*#__PURE__*/_interopDefaultLegacy(namesPlugin);
colord.extend([namesPlugin__default["default"]]);
const _Color = class {
"use strict";
var colord = require("colord"), namesPlugin = require("colord/plugins/names");
colord.extend([namesPlugin]);
const _Color = class _Color2 {
/**
* @param {PIXI.ColorSource} value - Optional value to use, if not provided, white is used.
*/
constructor(value = 16777215) {
this._value = null;
this._components = new Float32Array(4);
this._components.fill(1);
this._int = 16777215;
this.value = value;
this._value = null, this._components = new Float32Array(4), this._components.fill(1), this._int = 16777215, this.value = value;
}
/** Get red component (0 - 1) */
get red() {
return this._components[0];
}
/** Get green component (0 - 1) */
get green() {
return this._components[1];
}
/** Get blue component (0 - 1) */
get blue() {
return this._components[2];
}
/** Get alpha component (0 - 1) */
get alpha() {
return this._components[3];
}
/**
* Set the value, suitable for chaining
* @param value
* @see PIXI.Color.value
*/
setValue(value) {
this.value = value;
return this;
return this.value = value, this;
}
/**
* The current color source.
*
* When setting:
* - Setting to an instance of `Color` will copy its color source and components.
* - Otherwise, `Color` will try to normalize the color source and set the components.
* If the color source is invalid, an `Error` will be thrown and the `Color` will left unchanged.
*
* Note: The `null` in the setter's parameter type is added to match the TypeScript rule: return type of getter
* must be assignable to its setter's parameter type. Setting `value` to `null` will throw an `Error`.
*
* When getting:
* - A return value of `null` means the previous value was overridden (e.g., {@link PIXI.Color.multiply multiply},
* {@link PIXI.Color.premultiply premultiply} or {@link PIXI.Color.round round}).
* - Otherwise, the color source used when setting is returned.
* @type {PIXI.ColorSource}
*/
set value(value) {
if (value instanceof _Color) {
this._value = this.cloneSource(value._value);
this._int = value._int;
this._components.set(value._components);
} else if (value === null) {
throw new Error("Cannot set PIXI.Color#value to null");
} else if (this._value === null || !this.isSourceEqual(this._value, value)) {
this.normalize(value);
this._value = this.cloneSource(value);
if (value instanceof _Color2)
this._value = this.cloneSource(value._value), this._int = value._int, this._components.set(value._components);
else {
if (value === null)
throw new Error("Cannot set PIXI.Color#value to null");
(this._value === null || !this.isSourceEqual(this._value, value)) && (this.normalize(value), this._value = this.cloneSource(value));
}

@@ -52,34 +64,35 @@ }

}
/**
* Copy a color source internally.
* @param value - Color source
*/
cloneSource(value) {
if (typeof value === "string" || typeof value === "number" || value instanceof Number || value === null) {
return value;
} else if (Array.isArray(value) || ArrayBuffer.isView(value)) {
return value.slice(0);
} else if (typeof value === "object" && value !== null) {
return { ...value };
}
return value;
return typeof value == "string" || typeof value == "number" || value instanceof Number || value === null ? value : Array.isArray(value) || ArrayBuffer.isView(value) ? value.slice(0) : typeof value == "object" && value !== null ? { ...value } : value;
}
/**
* Equality check for color sources.
* @param value1 - First color source
* @param value2 - Second color source
* @returns `true` if the color sources are equal, `false` otherwise.
*/
isSourceEqual(value1, value2) {
const type1 = typeof value1;
const type2 = typeof value2;
if (type1 !== type2) {
return false;
} else if (type1 === "number" || type1 === "string" || value1 instanceof Number) {
if (type1 !== typeof value2)
return !1;
if (type1 === "number" || type1 === "string" || value1 instanceof Number)
return value1 === value2;
} else if (Array.isArray(value1) && Array.isArray(value2) || ArrayBuffer.isView(value1) && ArrayBuffer.isView(value2)) {
if (value1.length !== value2.length) {
return false;
}
return value1.every((v, i) => v === value2[i]);
} else if (value1 !== null && value2 !== null) {
const keys1 = Object.keys(value1);
const keys2 = Object.keys(value2);
if (keys1.length !== keys2.length) {
return false;
}
return keys1.every((key) => value1[key] === value2[key]);
if (Array.isArray(value1) && Array.isArray(value2) || ArrayBuffer.isView(value1) && ArrayBuffer.isView(value2))
return value1.length !== value2.length ? !1 : value1.every((v, i) => v === value2[i]);
if (value1 !== null && value2 !== null) {
const keys1 = Object.keys(value1), keys2 = Object.keys(value2);
return keys1.length !== keys2.length ? !1 : keys1.every((key) => value1[key] === value2[key]);
}
return value1 === value2;
}
/**
* Convert to a RGBA color object.
* @example
* import { Color } from 'pixi.js';
* new Color('white').toRgb(); // returns { r: 1, g: 1, b: 1, a: 1 }
*/
toRgba() {

@@ -89,2 +102,8 @@ const [r, g, b, a] = this._components;

}
/**
* Convert to a RGB color object.
* @example
* import { Color } from 'pixi.js';
* new Color('white').toRgb(); // returns { r: 1, g: 1, b: 1 }
*/
toRgb() {

@@ -94,2 +113,3 @@ const [r, g, b] = this._components;

}
/** Convert to a CSS-style rgba string: `rgba(255,255,255,1.0)`. */
toRgbaString() {

@@ -101,7 +121,3 @@ const [r, g, b] = this.toUint8RgbArray();

const [r, g, b] = this._components;
out = out ?? [];
out[0] = Math.round(r * 255);
out[1] = Math.round(g * 255);
out[2] = Math.round(b * 255);
return out;
return out = out ?? [], out[0] = Math.round(r * 255), out[1] = Math.round(g * 255), out[2] = Math.round(b * 255), out;
}

@@ -111,10 +127,20 @@ toRgbArray(out) {

const [r, g, b] = this._components;
out[0] = r;
out[1] = g;
out[2] = b;
return out;
return out[0] = r, out[1] = g, out[2] = b, out;
}
/**
* Convert to a hexadecimal number.
* @example
* import { Color } from 'pixi.js';
* new Color('white').toNumber(); // returns 16777215
*/
toNumber() {
return this._int;
}
/**
* Convert to a hexadecimal number in little endian format (e.g., BBGGRR).
* @example
* import { Color } from 'pixi.js';
* new Color(0xffcc99).toLittleEndianNumber(); // returns 0x99ccff
* @returns {number} - The color as a number in little endian format.
*/
toLittleEndianNumber() {

@@ -124,40 +150,41 @@ const value = this._int;

}
/**
* Multiply with another color. This action is destructive, and will
* override the previous `value` property to be `null`.
* @param {PIXI.ColorSource} value - The color to multiply by.
*/
multiply(value) {
const [r, g, b, a] = _Color.temp.setValue(value)._components;
this._components[0] *= r;
this._components[1] *= g;
this._components[2] *= b;
this._components[3] *= a;
this.refreshInt();
this._value = null;
return this;
const [r, g, b, a] = _Color2.temp.setValue(value)._components;
return this._components[0] *= r, this._components[1] *= g, this._components[2] *= b, this._components[3] *= a, this.refreshInt(), this._value = null, this;
}
premultiply(alpha, applyToRGB = true) {
if (applyToRGB) {
this._components[0] *= alpha;
this._components[1] *= alpha;
this._components[2] *= alpha;
}
this._components[3] = alpha;
this.refreshInt();
this._value = null;
return this;
/**
* Converts color to a premultiplied alpha format. This action is destructive, and will
* override the previous `value` property to be `null`.
* @param alpha - The alpha to multiply by.
* @param {boolean} [applyToRGB=true] - Whether to premultiply RGB channels.
* @returns {PIXI.Color} - Itself.
*/
premultiply(alpha, applyToRGB = !0) {
return applyToRGB && (this._components[0] *= alpha, this._components[1] *= alpha, this._components[2] *= alpha), this._components[3] = alpha, this.refreshInt(), this._value = null, this;
}
toPremultiplied(alpha, applyToRGB = true) {
if (alpha === 1) {
/**
* Premultiplies alpha with current color.
* @param {number} alpha - The alpha to multiply by.
* @param {boolean} [applyToRGB=true] - Whether to premultiply RGB channels.
* @returns {number} tint multiplied by alpha
*/
toPremultiplied(alpha, applyToRGB = !0) {
if (alpha === 1)
return (255 << 24) + this._int;
}
if (alpha === 0) {
if (alpha === 0)
return applyToRGB ? 0 : this._int;
}
let r = this._int >> 16 & 255;
let g = this._int >> 8 & 255;
let b = this._int & 255;
if (applyToRGB) {
r = r * alpha + 0.5 | 0;
g = g * alpha + 0.5 | 0;
b = b * alpha + 0.5 | 0;
}
return (alpha * 255 << 24) + (r << 16) + (g << 8) + b;
let r = this._int >> 16 & 255, g = this._int >> 8 & 255, b = this._int & 255;
return applyToRGB && (r = r * alpha + 0.5 | 0, g = g * alpha + 0.5 | 0, b = b * alpha + 0.5 | 0), (alpha * 255 << 24) + (r << 16) + (g << 8) + b;
}
/**
* Convert to a hexidecimal string.
* @example
* import { Color } from 'pixi.js';
* new Color('white').toHex(); // returns "#ffffff"
*/
toHex() {

@@ -167,19 +194,28 @@ const hexString = this._int.toString(16);

}
/**
* Convert to a hexidecimal string with alpha.
* @example
* import { Color } from 'pixi.js';
* new Color('white').toHexa(); // returns "#ffffffff"
*/
toHexa() {
const alphaValue = Math.round(this._components[3] * 255);
const alphaString = alphaValue.toString(16);
const alphaString = Math.round(this._components[3] * 255).toString(16);
return this.toHex() + "00".substring(0, 2 - alphaString.length) + alphaString;
}
/**
* Set alpha, suitable for chaining.
* @param alpha
*/
setAlpha(alpha) {
this._components[3] = this._clamp(alpha);
return this;
return this._components[3] = this._clamp(alpha), this;
}
/**
* Rounds the specified color according to the step. This action is destructive, and will
* override the previous `value` property to be `null`. The alpha component is not rounded.
* @param steps - Number of steps which will be used as a cap when rounding colors
* @deprecated since 7.3.0
*/
round(steps) {
const [r, g, b] = this._components;
this._components[0] = Math.round(r * steps) / steps;
this._components[1] = Math.round(g * steps) / steps;
this._components[2] = Math.round(b * steps) / steps;
this.refreshInt();
this._value = null;
return this;
return this._components[0] = Math.round(r * steps) / steps, this._components[1] = Math.round(g * steps) / steps, this._components[2] = Math.round(b * steps) / steps, this.refreshInt(), this._value = null, this;
}

@@ -189,54 +225,31 @@ toArray(out) {

const [r, g, b, a] = this._components;
out[0] = r;
out[1] = g;
out[2] = b;
out[3] = a;
return out;
return out[0] = r, out[1] = g, out[2] = b, out[3] = a, out;
}
/**
* Normalize the input value into rgba
* @param value - Input value
*/
normalize(value) {
let r;
let g;
let b;
let a;
if ((typeof value === "number" || value instanceof Number) && value >= 0 && value <= 16777215) {
let r, g, b, a;
if ((typeof value == "number" || value instanceof Number) && value >= 0 && value <= 16777215) {
const int = value;
r = (int >> 16 & 255) / 255;
g = (int >> 8 & 255) / 255;
b = (int & 255) / 255;
a = 1;
} else if ((Array.isArray(value) || value instanceof Float32Array) && value.length >= 3 && value.length <= 4) {
value = this._clamp(value);
[r, g, b, a = 1] = value;
} else if ((value instanceof Uint8Array || value instanceof Uint8ClampedArray) && value.length >= 3 && value.length <= 4) {
value = this._clamp(value, 0, 255);
[r, g, b, a = 255] = value;
r /= 255;
g /= 255;
b /= 255;
a /= 255;
} else if (typeof value === "string" || typeof value === "object") {
if (typeof value === "string") {
const match = _Color.HEX_PATTERN.exec(value);
if (match) {
value = `#${match[2]}`;
}
r = (int >> 16 & 255) / 255, g = (int >> 8 & 255) / 255, b = (int & 255) / 255, a = 1;
} else if ((Array.isArray(value) || value instanceof Float32Array) && value.length >= 3 && value.length <= 4)
value = this._clamp(value), [r, g, b, a = 1] = value;
else if ((value instanceof Uint8Array || value instanceof Uint8ClampedArray) && value.length >= 3 && value.length <= 4)
value = this._clamp(value, 0, 255), [r, g, b, a = 255] = value, r /= 255, g /= 255, b /= 255, a /= 255;
else if (typeof value == "string" || typeof value == "object") {
if (typeof value == "string") {
const match = _Color2.HEX_PATTERN.exec(value);
match && (value = `#${match[2]}`);
}
const color = colord.colord(value);
if (color.isValid()) {
({ r, g, b, a } = color.rgba);
r /= 255;
g /= 255;
b /= 255;
}
color.isValid() && ({ r, g, b, a } = color.rgba, r /= 255, g /= 255, b /= 255);
}
if (r !== void 0) {
this._components[0] = r;
this._components[1] = g;
this._components[2] = b;
this._components[3] = a;
this.refreshInt();
} else {
if (r !== void 0)
this._components[0] = r, this._components[1] = g, this._components[2] = b, this._components[3] = a, this.refreshInt();
else
throw new Error(`Unable to convert color ${value}`);
}
}
/** Refresh the internal color rgb number */
refreshInt() {

@@ -247,18 +260,23 @@ this._clamp(this._components);

}
/**
* Clamps values to a range. Will override original values
* @param value - Value(s) to clamp
* @param min - Minimum value
* @param max - Maximum value
*/
_clamp(value, min = 0, max = 1) {
if (typeof value === "number") {
return Math.min(Math.max(value, min), max);
}
value.forEach((v, i) => {
return typeof value == "number" ? Math.min(Math.max(value, min), max) : (value.forEach((v, i) => {
value[i] = Math.min(Math.max(v, min), max);
});
return value;
}), value);
}
};
_Color.shared = new _Color(), /**
* Temporary Color object for static uses internally.
* As to not conflict with Color.shared.
* @ignore
*/
_Color.temp = new _Color(), /** Pattern for hex strings */
_Color.HEX_PATTERN = /^(#|0x)?(([a-f0-9]{3}){1,2}([a-f0-9]{2})?)$/i;
let Color = _Color;
Color.shared = new _Color();
Color.temp = new _Color();
Color.HEX_PATTERN = /^(#|0x)?(([a-f0-9]{3}){1,2}([a-f0-9]{2})?)$/i;
exports.Color = Color;
//# sourceMappingURL=Color.js.map

@@ -1,10 +0,4 @@

'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
var Color = require('./Color.js');
"use strict";
var Color = require("./Color.js");
exports.Color = Color.Color;
//# sourceMappingURL=index.js.map
{
"name": "@pixi/color",
"version": "7.2.4",
"version": "7.3.0-rc",
"main": "lib/index.js",

@@ -5,0 +5,0 @@ "module": "lib/index.mjs",

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc