all files / src/extractor/ rgb-percent-extractor.js

100% Statements 63/63
100% Branches 26/26
100% Functions 11/11
100% Lines 13/13
22 statements, 1 function, 17 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                     106131× 106131×   14× 14× 14×   14× 42×   14×        
import { Color } from "../color";
import { RgbPercentDetector } from "../detector/rgb-percent-detector";
 
/**
 * %指定RGB表現の抽出処理を提供します。
 */
class RgbPercentExtractor {
    /**
     * 色情報を抽出します。
     * @param {String} expression WEBカラーの色表現。
     * @return {Color} 抽出結果を返します。
     */
    extract(expression) {
        const detector = new RgbPercentDetector();
        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 [ red, green, blue ] = values.slice(0, 3).map(x => Math.round(Number(x.replace(/[^0-9.]/g, "") / 100 * 255)));
 
        return new Color(red, green, blue, null);
    }
}
 
export { RgbPercentExtractor }