all files / src/color-expression-converter/parts/ rgb.js

68.33% Statements 41/60
100% Branches 29/29
80% Functions 8/10
24% Lines 6/25
4 statements, 13 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 46 47                                                                                    
import { Color } from "./color";
import { ExpressionType } from "./expression-type";
 
/**
 * RGB表現に関連する処理を提供します。
 */
class Rgb extends ExpressionType {
    toColor (expression) {
        try{
            let ret = new Color();
            ret.origin = expression;
 
            expression = expression.trim();
 
            let target = expression.substring(expression.indexOf("(") + 1)
            target = target.substring(0, target.indexOf(")")).replace(" ", "");
            let targets = target.split(",");
 
            ret.red   = parseInt(targets[0]);
            ret.green = parseInt(targets[1]);
            ret.blue  = parseInt(targets[2]);
            ret.alpah = 1;
 
            return ret;
        }catch(ex){
            throw ex;
        }
    }
 
    createColorExpression (color) {
        try{
            let red   = color.red;
            let green = color.green;
            let blue  = color.blue;
 
            return `RGB(${red}, ${green}, ${blue})`;
        }catch(ex){
            throw ex;
        }
    }
}
 
// シングルトンとして提供
const instance = new Rgb();
export { instance as Rgb };