all files / src/extractor/ hsl-extractor.js

100% Statements 50/50
100% Branches 12/12
100% Functions 11/11
100% Lines 23/23
2 statements, 4 branches Ignored     
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45                     419743× 419743×   250098× 250098× 250098×   250098× 250098× 250098× 250098×         250098× 250098× 250098× 1500588× 250098× 750294× 250098×       750294× 250098×        
import { Color } from "../color";
import { HslDetector } from "../detector/hsl-detector";
 
/**
 * HSL表現の抽出処理を提供します。
 */
class HslExtractor {
    /**
     * 色情報を抽出します。
     * @param {String} expression WEBカラーの色表現。
     * @return {Color} 抽出結果を返します。
     */
    extract(expression) {
        const detector = new HslDetector();
        if (!detector.match(expression)) return null;
 
        const head = expression.indexOf("(");
        const tail = expression.indexOf(")");
        const body = expression.slice(head + 1, tail);
 
        const values = body.split(",");
        const hue = Number(values[0].trim());
        const saturation = Number(values[1].replace(/[^0-9.]/g, ""));
        const lightness = Number(values[2].replace(/[^0-9.]/g, ""));
 
        /* ---------------------------------------------------------------------- */
        // 30-seconds-of-code (Licensed under CC BY 4.0)
        // https://www.30secondsofcode.org/js/s/hsl-to-rgb
        const hslToRgb = (h, s, l) => {
            s /= 100;
            l /= 100;
            const k = n => (n + h / 30) % 12;
            const a = s * Math.min(l, 1 - l);
            const f = n => l - a * Math.max(-1, Math.min(k(n) - 3, Math.min(9 - k(n), 1)));
            return [255 * f(0), 255 * f(8), 255 * f(4)];
        };
        /* ---------------------------------------------------------------------- */
 
        const rgb = hslToRgb(hue, saturation, lightness).map(x => Math.round(x));
        return new Color(rgb[0], rgb[1], rgb[2], null);
    }
}
 
export { HslExtractor };