@uiw/color-convert
Advanced tools
Comparing version 2.0.5 to 2.0.6
@@ -1,3 +0,1 @@ | ||
export * from './utils'; | ||
export * from './getContrastingColor'; | ||
export type ObjectColor = RgbColor | HslColor | HsvColor | RgbaColor | HslaColor | HsvaColor; | ||
@@ -76,1 +74,6 @@ export type ColorResult = { | ||
export declare const color: (str: string | HsvaColor) => ColorResult; | ||
export declare const getContrastingColor: (str: string | HsvaColor) => "#ffffff" | "#000000"; | ||
export declare const equalColorObjects: (first: ObjectColor, second: ObjectColor) => boolean; | ||
export declare const equalColorString: (first: string, second: string) => boolean; | ||
export declare const equalHex: (first: string, second: string) => boolean; | ||
export declare const validHex: (hex: string) => boolean; |
@@ -7,59 +7,5 @@ "use strict"; | ||
}); | ||
var _exportNames = { | ||
rgbaToHsva: true, | ||
hsvaToHslString: true, | ||
hsvaToHsvString: true, | ||
hsvaToHsvaString: true, | ||
hsvaToHslaString: true, | ||
hslStringToHsla: true, | ||
hslaStringToHsva: true, | ||
hslStringToHsva: true, | ||
hslaToHsva: true, | ||
hsvaToHsla: true, | ||
hsvaStringToHsva: true, | ||
parseHue: true, | ||
hsvStringToHsva: true, | ||
rgbaStringToHsva: true, | ||
rgbStringToHsva: true, | ||
rgbaToHex: true, | ||
rgbaToHexa: true, | ||
hexToHsva: true, | ||
hexToRgba: true, | ||
hsvaToRgba: true, | ||
hsvaToRgbString: true, | ||
hsvaToRgbaString: true, | ||
rgbaToRgb: true, | ||
hslaToHsl: true, | ||
hsvaToHex: true, | ||
hsvaToHexa: true, | ||
hsvaToHsv: true, | ||
color: true | ||
}; | ||
exports.rgbaToRgb = exports.rgbaToHsva = exports.rgbaToHexa = exports.rgbaToHex = exports.rgbaStringToHsva = exports.rgbStringToHsva = exports.parseHue = exports.hsvaToRgbaString = exports.hsvaToRgba = exports.hsvaToRgbString = exports.hsvaToHsvaString = exports.hsvaToHsvString = exports.hsvaToHsv = exports.hsvaToHslaString = exports.hsvaToHsla = exports.hsvaToHslString = exports.hsvaToHexa = exports.hsvaToHex = exports.hsvaStringToHsva = exports.hsvStringToHsva = exports.hslaToHsva = exports.hslaToHsl = exports.hslaStringToHsva = exports.hslStringToHsva = exports.hslStringToHsla = exports.hexToRgba = exports.hexToHsva = exports.color = void 0; | ||
exports.validHex = exports.rgbaToRgb = exports.rgbaToHsva = exports.rgbaToHexa = exports.rgbaToHex = exports.rgbaStringToHsva = exports.rgbStringToHsva = exports.parseHue = exports.hsvaToRgbaString = exports.hsvaToRgba = exports.hsvaToRgbString = exports.hsvaToHsvaString = exports.hsvaToHsvString = exports.hsvaToHsv = exports.hsvaToHslaString = exports.hsvaToHsla = exports.hsvaToHslString = exports.hsvaToHexa = exports.hsvaToHex = exports.hsvaStringToHsva = exports.hsvStringToHsva = exports.hslaToHsva = exports.hslaToHsl = exports.hslaStringToHsva = exports.hslStringToHsva = exports.hslStringToHsla = exports.hexToRgba = exports.hexToHsva = exports.getContrastingColor = exports.equalHex = exports.equalColorString = exports.equalColorObjects = exports.color = void 0; | ||
var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2")); | ||
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray")); | ||
var _utils = require("./utils"); | ||
Object.keys(_utils).forEach(function (key) { | ||
if (key === "default" || key === "__esModule") return; | ||
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return; | ||
if (key in exports && exports[key] === _utils[key]) return; | ||
Object.defineProperty(exports, key, { | ||
enumerable: true, | ||
get: function get() { | ||
return _utils[key]; | ||
} | ||
}); | ||
}); | ||
var _getContrastingColor = require("./getContrastingColor"); | ||
Object.keys(_getContrastingColor).forEach(function (key) { | ||
if (key === "default" || key === "__esModule") return; | ||
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return; | ||
if (key in exports && exports[key] === _getContrastingColor[key]) return; | ||
Object.defineProperty(exports, key, { | ||
enumerable: true, | ||
get: function get() { | ||
return _getContrastingColor[key]; | ||
} | ||
}); | ||
}); | ||
var RGB_MAX = 255; | ||
@@ -392,3 +338,3 @@ var HUE_MAX = 360; | ||
var hexa; | ||
if (typeof str === 'string' && (0, _utils.validHex)(str)) { | ||
if (typeof str === 'string' && validHex(str)) { | ||
hsva = hexToHsva(str); | ||
@@ -418,2 +364,34 @@ hex = str; | ||
}; | ||
}; | ||
var getContrastingColor = exports.getContrastingColor = function getContrastingColor(str) { | ||
if (!str) { | ||
return '#ffffff'; | ||
} | ||
var col = color(str); | ||
var yiq = (col.rgb.r * 299 + col.rgb.g * 587 + col.rgb.b * 114) / 1000; | ||
return yiq >= 128 ? '#000000' : '#ffffff'; | ||
}; | ||
var equalColorObjects = exports.equalColorObjects = function equalColorObjects(first, second) { | ||
if (first === second) return true; | ||
for (var prop in first) { | ||
// The following allows for a type-safe calling of this function (first & second have to be HSL, HSV, or RGB) | ||
// with type-unsafe iterating over object keys. TS does not allow this without an index (`[key: string]: number`) | ||
// on an object to define how iteration is normally done. To ensure extra keys are not allowed on our types, | ||
// we must cast our object to unknown (as RGB demands `r` be a key, while `Record<string, x>` does not care if | ||
// there is or not), and then as a type TS can iterate over. | ||
if (first[prop] !== second[prop]) return false; | ||
} | ||
return true; | ||
}; | ||
var equalColorString = exports.equalColorString = function equalColorString(first, second) { | ||
return first.replace(/\s/g, '') === second.replace(/\s/g, ''); | ||
}; | ||
var equalHex = exports.equalHex = function equalHex(first, second) { | ||
if (first.toLowerCase() === second.toLowerCase()) return true; | ||
// To compare colors like `#FFF` and `ffffff` we convert them into RGB objects | ||
return equalColorObjects(hexToRgba(first), hexToRgba(second)); | ||
}; | ||
var validHex = exports.validHex = function validHex(hex) { | ||
return /^#?([A-Fa-f0-9]{3,4}){1,2}$/.test(hex); | ||
}; |
@@ -1,3 +0,1 @@ | ||
export * from './utils'; | ||
export * from './getContrastingColor'; | ||
export type ObjectColor = RgbColor | HslColor | HsvColor | RgbaColor | HslaColor | HsvaColor; | ||
@@ -76,1 +74,6 @@ export type ColorResult = { | ||
export declare const color: (str: string | HsvaColor) => ColorResult; | ||
export declare const getContrastingColor: (str: string | HsvaColor) => "#ffffff" | "#000000"; | ||
export declare const equalColorObjects: (first: ObjectColor, second: ObjectColor) => boolean; | ||
export declare const equalColorString: (first: string, second: string) => boolean; | ||
export declare const equalHex: (first: string, second: string) => boolean; | ||
export declare const validHex: (hex: string) => boolean; |
import _extends from "@babel/runtime/helpers/extends"; | ||
import { validHex } from './utils'; | ||
export * from './utils'; | ||
export * from './getContrastingColor'; | ||
var RGB_MAX = 255; | ||
@@ -364,2 +361,32 @@ var HUE_MAX = 360; | ||
}; | ||
}; | ||
}; | ||
export var getContrastingColor = str => { | ||
if (!str) { | ||
return '#ffffff'; | ||
} | ||
var col = color(str); | ||
var yiq = (col.rgb.r * 299 + col.rgb.g * 587 + col.rgb.b * 114) / 1000; | ||
return yiq >= 128 ? '#000000' : '#ffffff'; | ||
}; | ||
export var equalColorObjects = (first, second) => { | ||
if (first === second) return true; | ||
for (var prop in first) { | ||
// The following allows for a type-safe calling of this function (first & second have to be HSL, HSV, or RGB) | ||
// with type-unsafe iterating over object keys. TS does not allow this without an index (`[key: string]: number`) | ||
// on an object to define how iteration is normally done. To ensure extra keys are not allowed on our types, | ||
// we must cast our object to unknown (as RGB demands `r` be a key, while `Record<string, x>` does not care if | ||
// there is or not), and then as a type TS can iterate over. | ||
if (first[prop] !== second[prop]) return false; | ||
} | ||
return true; | ||
}; | ||
export var equalColorString = (first, second) => { | ||
return first.replace(/\s/g, '') === second.replace(/\s/g, ''); | ||
}; | ||
export var equalHex = (first, second) => { | ||
if (first.toLowerCase() === second.toLowerCase()) return true; | ||
// To compare colors like `#FFF` and `ffffff` we convert them into RGB objects | ||
return equalColorObjects(hexToRgba(first), hexToRgba(second)); | ||
}; | ||
export var validHex = hex => /^#?([A-Fa-f0-9]{3,4}){1,2}$/.test(hex); |
{ | ||
"name": "@uiw/color-convert", | ||
"version": "2.0.5", | ||
"version": "2.0.6", | ||
"description": "Color Convert", | ||
@@ -5,0 +5,0 @@ "author": "Kenny Wong <wowohoo@qq.com>", |
@@ -1,6 +0,1 @@ | ||
import { validHex } from './utils'; | ||
export * from './utils'; | ||
export * from './getContrastingColor'; | ||
const RGB_MAX = 255; | ||
@@ -308,1 +303,38 @@ const HUE_MAX = 360; | ||
}; | ||
export const getContrastingColor = (str: string | HsvaColor) => { | ||
if (!str) { | ||
return '#ffffff'; | ||
} | ||
const col = color(str); | ||
const yiq = (col.rgb.r * 299 + col.rgb.g * 587 + col.rgb.b * 114) / 1000; | ||
return yiq >= 128 ? '#000000' : '#ffffff'; | ||
}; | ||
export const equalColorObjects = (first: ObjectColor, second: ObjectColor): boolean => { | ||
if (first === second) return true; | ||
for (const prop in first) { | ||
// The following allows for a type-safe calling of this function (first & second have to be HSL, HSV, or RGB) | ||
// with type-unsafe iterating over object keys. TS does not allow this without an index (`[key: string]: number`) | ||
// on an object to define how iteration is normally done. To ensure extra keys are not allowed on our types, | ||
// we must cast our object to unknown (as RGB demands `r` be a key, while `Record<string, x>` does not care if | ||
// there is or not), and then as a type TS can iterate over. | ||
if ((first as unknown as Record<string, number>)[prop] !== (second as unknown as Record<string, number>)[prop]) return false; | ||
} | ||
return true; | ||
}; | ||
export const equalColorString = (first: string, second: string): boolean => { | ||
return first.replace(/\s/g, '') === second.replace(/\s/g, ''); | ||
}; | ||
export const equalHex = (first: string, second: string): boolean => { | ||
if (first.toLowerCase() === second.toLowerCase()) return true; | ||
// To compare colors like `#FFF` and `ffffff` we convert them into RGB objects | ||
return equalColorObjects(hexToRgba(first), hexToRgba(second)); | ||
}; | ||
export const validHex = (hex: string): boolean => /^#?([A-Fa-f0-9]{3,4}){1,2}$/.test(hex); |
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
47177
7
1223