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

100% Statements 38/38
100% Branches 12/12
100% Functions 8/8
100% Lines 13/13
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                     481900× 481900×   20×        
import { Color } from "../color";
import { Hex4Detector } from "../detector/hex4-detector";
 
/**
 * 4桁の16進数表現の抽出処理を提供します。
 */
class Hex4Extractor {
    /**
     * 色情報を抽出します。
     * @param {String} expression WEBカラーの色表現。
     * @return {Color} 抽出結果を返します。
     */
    extract(expression) {
        const detector = new Hex4Detector();
        if (!detector.match(expression)) return null;
 
        const getValue = hex => parseInt(hex.repeat(2), 16);
        const r = getValue(expression.slice(1, 2));
        const g = getValue(expression.slice(2, 3));
        const b = getValue(expression.slice(3, 4));
        const a = getValue(expression.slice(4, 5));
        return new Color(r, g, b, Math.round(a / 255 * 100));
    }
}
 
export { Hex4Extractor };