graphics-ts
Version:
A porting of purescript-{canvas, free-canvas, drawing} featuring fp-ts
67 lines (66 loc) • 2.15 kB
JavaScript
;
/**
* @since 1.0.0
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.toCss = exports.white = exports.black = exports.hsl = exports.hsla = exports.hex = void 0;
// -------------------------------------------------------------------------------------
// constructors
// -------------------------------------------------------------------------------------
/**
* Constructs a `Color` using a hexadecimal value.
*
* @category constructors
* @since 1.0.0
*/
exports.hex = function (value) { return ({ _tag: 'Hex', value: value }); };
/**
* Constructs a `Color` using the specified hue, saturation, lightness, and alpha.
*
* @category constructors
* @since 1.0.0
*/
exports.hsla = function (h, s, l, a) { return ({ _tag: 'Hsla', h: h, s: s, l: l, a: a }); };
/**
* Constructs a fully opaque `Color` using the specified hue, saturation, and lightness.
*
* @category constructors
* @since 1.0.0
*/
exports.hsl = function (h, s, l) { return exports.hsla(h, s, l, 1); };
/**
* @category constructors
* @since 1.0.0
*/
exports.black = exports.hsl(0, 0, 0);
/**
* @category constructors
* @since 1.0.0
*/
exports.white = exports.hsl(360, 1, 1);
// -------------------------------------------------------------------------------------
// destructors
// -------------------------------------------------------------------------------------
/**
* Converts a `Color` into a valid CSS string.
*
* @category destructors
* @since 1.0.0
*/
exports.toCss = function (c) {
switch (c._tag) {
case 'Hex':
return c.value;
case 'Hsla': {
var h = c.h, s = c.s, l = c.l, a = c.a;
var toString_1 = function (n) { return Math.round(n * 100.0) / 100; };
var hue = toString_1(h);
var saturation = toString_1(s * 100.0) + '%';
var lightness = toString_1(l * 100.0) + '%';
var alpha = String(a);
return a === 1
? "hsl(" + hue + ", " + saturation + ", " + lightness + ")"
: "hsla(" + hue + ", " + saturation + ", " + lightness + ", " + alpha + ")";
}
}
};