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

100% Statements 39/39
100% Branches 12/12
100% Functions 7/7
100% Lines 15/15
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                     107382× 107382×   1118× 1118× 1118×   1118× 1118× 1118× 1118×   1118×        
import { Color } from "../color";
import { RgbDetector } from "../detector/rgb-detector";
 
/**
 * RGB表現の抽出処理を提供します。
 */
class RgbExtractor {
    /**
     * 色情報を抽出します。
     * @param {String} expression WEBカラーの色表現。
     * @return {Color} 抽出結果を返します。
     */
    extract(expression) {
        const detector = new RgbDetector();
        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 = Number(values[0].replace(/[^0-9.]/g, ""));
        const green = Number(values[1].replace(/[^0-9.]/g, ""));
        const blue = Number(values[2].replace(/[^0-9.]/g, ""));
 
        return new Color(red, green, blue, null);
    }
}
 
export { RgbExtractor }