@daeinc/color
Advanced tools
Comparing version 0.1.1 to 0.2.0
@@ -25,6 +25,33 @@ /** | ||
export declare const hsb2rgb: typeof hsv2rgb; | ||
/** | ||
* convert RGB color to normalized HSV | ||
* @param arr [r, g, b] or [r, g, b, a] | ||
* @returns HSV color array[3 or 4] in 0..1 | ||
*/ | ||
export declare function rgb2hsv(arr: number[]): number[]; | ||
/** | ||
* convert RGB color to normalized HSV | ||
* @param r red 0..255 | ||
* @param g green 0..255 | ||
* @param b blue 0..255 | ||
* @returns HSV color array[3] in 0..1 | ||
*/ | ||
export declare function rgb2hsv(r: number, g: number, b: number): number[]; | ||
/** | ||
* convert RGB color to normalized HSV | ||
* @param r red 0..255 | ||
* @param g green 0..255 | ||
* @param b blue 0..255 | ||
* @param a alpha 0..1 | ||
* @returns HSV color array[4] in 0..1 | ||
*/ | ||
export declare function rgb2hsv(r: number, g: number, b: number, a: number): number[]; | ||
export declare function hex2rgb(): void; | ||
/** | ||
* converts hexadecimal color to RGB/RGBA. | ||
* | ||
* It also supports shorthand hex color with alpha - ex. #f0ac (last char is alpha) | ||
* @param hex hex color string. ex. `#f0a`, `#ccfa`, `#ff00cc` or `#aacc99ff` | ||
* @returns [r, g, b] or [r, g, b, a] | ||
*/ | ||
export declare function hex2rgb(hex: string): number[]; | ||
//# sourceMappingURL=index.d.ts.map |
@@ -5,2 +5,3 @@ "use strict"; | ||
const hsvrgb = require("hsv2rgb"); | ||
const rgbhsv = require("rgb-hsv"); | ||
/** | ||
@@ -24,8 +25,9 @@ * convert normalized HSB color to RGB | ||
else if (s === undefined) { | ||
// ([h, s, v]) or ([h, s, v, a]) | ||
hOrArr[0] *= 360; | ||
if (hOrArr.length === 3) { | ||
// ([h, s, v]) | ||
return [...hsvrgb(...hOrArr)]; | ||
} | ||
else if (hOrArr.length === 4) { | ||
// ([h, s, v, a]) | ||
// hsvrgb's 4th argument is NOT alpha but out array | ||
@@ -39,10 +41,72 @@ return [...hsvrgb(...hOrArr.slice(0, 3)), hOrArr[3]]; | ||
exports.hsb2rgb = hsv2rgb; | ||
/** | ||
* convert RGB color to normalized HSV | ||
* @param rOrArr red 0..255 | ||
* @param g green 0..255 | ||
* @param b blue 0..255 | ||
* @param a alpha 0..1 | ||
* @returns HSV color array in 0..1 | ||
*/ | ||
function rgb2hsv(rOrArr, g, b, a) { | ||
return []; | ||
if (g !== undefined && b !== undefined && a !== undefined) { | ||
// (r, g, b, a) | ||
const v = rgbhsv(rOrArr, g, b); | ||
return [v[0] / 360, v[1] / 100, v[2] / 100, a]; | ||
} | ||
else if (g !== undefined && b !== undefined) { | ||
// (r, g, b) | ||
const v = rgbhsv(rOrArr, g, b); | ||
return [v[0] / 360, v[1] / 100, v[2] / 100]; | ||
} | ||
else if (g === undefined) { | ||
if (rOrArr.length === 3) { | ||
// [(r, g, b)] | ||
const v = rgbhsv(...rOrArr); | ||
return [v[0] / 360, v[1] / 100, v[2] / 100]; | ||
} | ||
else if (rOrArr.length === 4) { | ||
// [(r, g, b, a)] | ||
const v = rgbhsv(...rOrArr.slice(0, 3)); | ||
return [v[0] / 360, v[1] / 100, v[2] / 100, rOrArr[3]]; | ||
} | ||
} | ||
throw new Error("rgb2hsv(): doesn't support the type or length of arguments provided"); | ||
} | ||
exports.rgb2hsv = rgb2hsv; | ||
function hex2rgb() { | ||
// | ||
/** | ||
* converts hexadecimal color to RGB/RGBA. | ||
* | ||
* It also supports shorthand hex color with alpha - ex. #f0ac (last char is alpha) | ||
* @param hex hex color string. ex. `#f0a`, `#ccfa`, `#ff00cc` or `#aacc99ff` | ||
* @returns [r, g, b] or [r, g, b, a] | ||
*/ | ||
function hex2rgb(hex) { | ||
let rgb; | ||
if (hex.length === 7 || hex.length === 9) { | ||
// #rrggbb or #rrbbggaa | ||
rgb = hex.substring(1, 9).match(/.{2}/g); | ||
if (rgb) { | ||
return rgb.map((val, i) => { | ||
let newval = parseInt(val, 16); | ||
if (i === 3) | ||
newval /= 255; // normalize alpha channel | ||
return newval; | ||
}); | ||
} | ||
} | ||
else if (hex.length === 4 || hex.length === 5) { | ||
// #rgb or #rgba | ||
rgb = hex.substring(1, 5).match(/.{1}/g); | ||
if (rgb) { | ||
return rgb.map((val, i) => { | ||
let newval = parseInt(val + val, 16); // repeat twice for shorthand hex value | ||
if (i === 3) | ||
newval /= 255; | ||
return newval; | ||
}); | ||
} | ||
} | ||
throw new Error("hex2rgb(): doesn't support the length or value of string argument provided"); | ||
} | ||
exports.hex2rgb = hex2rgb; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@daeinc/color", | ||
"version": "0.1.1", | ||
"version": "0.2.0", | ||
"description": "Color utilities", | ||
@@ -19,3 +19,4 @@ "types": "./dist/index.d.ts", | ||
"dependencies": { | ||
"hsv2rgb": "^1.1.0" | ||
"hsv2rgb": "^1.1.0", | ||
"rgb-hsv": "^1.0.0" | ||
}, | ||
@@ -22,0 +23,0 @@ "devDependencies": { |
# @daeinc/color | ||
color utilities | ||
Color utilities. Mostly wrapper functions on existing packages. | ||
## To dos | ||
- add `rgb2hsv()` | ||
- add `hex2rgb()` | ||
- back to back conversion does not return the original value. ex. `hsv2rgb(rgb2hsv(128, 127, 108))` | ||
- not equal: `expect(hex2rgb("#80808080")[3]).toBeCloseTo(0.5);` | ||
- add `rgb2hsl` and `hsl2rgb` | ||
@@ -10,0 +11,0 @@ ## License |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
12109
163
14
2
+ Addedrgb-hsv@^1.0.0
+ Addedrgb-hsv@1.0.0(transitive)