-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
v0.1.0 Adds effects builders and QOL changes to utils
- Loading branch information
Showing
5 changed files
with
302 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
'use strict' | ||
|
||
const { | ||
isHex, | ||
isRGB, | ||
isHSV, | ||
hex2RGB, | ||
HSV2RGB, | ||
RGB2Hex, | ||
RGB2HSV | ||
} = require('./utils'); | ||
|
||
module.exports = { | ||
shade, | ||
tint, | ||
tone | ||
}; | ||
|
||
function shade (base, format, step, count) { | ||
if (!base) throw new Error("Error: arg `base` is required!") | ||
|
||
if (!format) format = "hex"; | ||
if (!step) step = 0.10; | ||
if (!count) count = 5; | ||
|
||
return effectsBuilder(base, format, step, count, (rgb, shift) => { | ||
return { | ||
r: Math.min(Math.round((+rgb.r) * (1 - shift)), 255), | ||
g: Math.min(Math.round((+rgb.g) * (1 - shift)), 255), | ||
b: Math.min(Math.round((+rgb.b) * (1 - shift)), 255), | ||
shift | ||
} | ||
}); | ||
} | ||
|
||
function tint (base, format, step, count) { | ||
if (!base) throw new Error("Error: arg `base` is required!") | ||
|
||
if (!format) format = "hex"; | ||
if (!step) step = 0.10; | ||
if (!count) count = 5; | ||
|
||
return effectsBuilder(base, format, step, count, (rgb, shift) => { | ||
return { | ||
r: Math.min(Math.round((255 - (+rgb.r)) * +shift), 255), | ||
g: Math.min(Math.round((255 - (+rgb.g)) * +shift), 255), | ||
b: Math.min(Math.round((255 - (+rgb.b)) * +shift), 255), | ||
shift | ||
} | ||
}); | ||
} | ||
|
||
function tone (base, format, step, count) { | ||
if (!base) throw new Error("Error: arg `base` is required!") | ||
|
||
if (!format) format = "hex"; | ||
if (!step) step = 0.10; | ||
if (!count) count = 5; | ||
|
||
return effectsBuilder(base, format, step, count, (rgb, shift) => { | ||
return { | ||
r: Math.min(Math.round((+rgb.r) + (+rgb.r) * (0.5 - shift)), 255), | ||
g: Math.min(Math.round((+rgb.g) + (+rgb.g) * (0.5 - shift)), 255), | ||
b: Math.min(Math.round((+rgb.b) + (+rgb.b) * (0.5 - shift)), 255), | ||
shift | ||
} | ||
}); | ||
} | ||
|
||
|
||
function effectsBuilder (base, format="hex", step=0.05, count=5, applyEffect) { | ||
if (isNaN(step) || isNaN(count)) { | ||
throw new Error(`Arguments step and count must be of type number (or coercible). step=${step} count=${count}`) | ||
} | ||
|
||
if (step < 0.01) { | ||
throw new Error(`Shade step percision must be >= 0.01 (1%). step=${step}`); | ||
} | ||
|
||
const formatMap = { | ||
"hex": RGB2Hex, | ||
"hsv": RGB2HSV, | ||
"rgb": ((e) => e) | ||
} | ||
|
||
const constructor = base?.constructor?.name | ||
|
||
if (constructor === "Array") { | ||
const _results = {}; | ||
|
||
let i = 0; | ||
for(; i < count; i++) { | ||
const shift = Math.round(i * step * 100) / 100; | ||
|
||
_results[String(shift)] = base.map((color, index) => { | ||
let hex, hsv; | ||
if ( | ||
(color?.constructor?.name === "Object" || color?.constructor?.name === "String") && | ||
(isRGB(color) || (hex = isHex(color)) || (hsv = isHSV(color))) | ||
) { | ||
if (hex) color = hex2RGB(color); | ||
if (hsv) color = HSV2RGB(color); | ||
return formatMap[format](applyEffect(color, shift)); | ||
} | ||
|
||
throw new Error(`Invalid color format; unable to generate shades. base, index=${index} color=${color}`) | ||
}); | ||
} | ||
|
||
return _results; | ||
} | ||
|
||
// valid for hsv or rgb | ||
let hsv; | ||
if (constructor === "Object" && (isRGB(base) || (hsv = isHSV(base)))) { | ||
if (hsv) base = HSV2RGB(base); | ||
|
||
const _results = []; | ||
|
||
let i = 0; | ||
for(; i < count; i++) { | ||
const shift = Math.round(i * step * 100) / 100; | ||
_results.push(formatMap[format](applyEffect(base, shift))); | ||
} | ||
|
||
return _results; | ||
} | ||
|
||
// only valid for singular hex color | ||
if (base.constructor.name === "String") { | ||
if (base.includes('#')) base = base.replace('#', ''); | ||
|
||
if (isHex(base)) { | ||
const rgb = hex2RGB(base); | ||
const _results = []; | ||
|
||
let i = 0; | ||
for(; i < count; i++) { | ||
const shift = Math.round(i * step * 100) / 100; | ||
_results.push(formatMap[format](applyEffect(rgb, shift))); | ||
} | ||
|
||
return _results; | ||
} | ||
|
||
throw new Error(`Invalid color format; unable to generate shades. base=${base}`) | ||
} | ||
|
||
throw new Error(`Shade generator requires a valid base color, or array of colors. \nbase.constructor.name=${base?.constructor?.name}`) | ||
} |
Oops, something went wrong.