Browse Source

更新scolor

haojianlong 5 years ago
parent
commit
a326ed10cf
2 changed files with 76 additions and 48 deletions
  1. 1 1
      saga-web-draw/package.json
  2. 75 47
      saga-web-draw/src/SColor.ts

+ 1 - 1
saga-web-draw/package.json

@@ -1,6 +1,6 @@
 {
     "name": "@saga-web/draw",
-    "version": "2.1.99",
+    "version": "2.1.100",
     "description": "上格云绘制引擎。",
     "main": "lib/index.js",
     "types": "lib/index.d.js",

+ 75 - 47
saga-web-draw/src/SColor.ts

@@ -6,24 +6,24 @@ import { SStringUtil } from "@saga-web/base/lib";
  * @author  庞利祥(sybotan@126.com)
  */
 export class SColor {
-    static Transparent = new SColor("#00000000");
-    static Black = new SColor("#000000");
-    static DarkBlue = new SColor("#000080");
-    static Blue = new SColor("#0000FF");
-    static DarkGreen = new SColor("#008000");
-    static Green = new SColor("#00FF00");
-    static DarkCyan = new SColor("#008080");
-    static Cyan = new SColor("#00FFFF");
-    static DarkRed = new SColor("#800000");
-    static Red = new SColor("#FF0000");
-    static DarkMagenta = new SColor("#800080");
-    static Magenta = new SColor("#FF00FF");
-    static DarkYellow = new SColor("#808000");
-    static Yellow = new SColor("#FFFF00");
-    static White = new SColor("#FFFFFF");
-    static DarkGray = new SColor("#808080");
-    static Gray = new SColor("#A0A0A0");
-    static LightGray = new SColor("#C0C0C0");
+    static readonly Transparent = new SColor("#00000000");
+    static readonly Black = new SColor("#000000");
+    static readonly DarkBlue = new SColor("#000080");
+    static readonly Blue = new SColor("#0000FF");
+    static readonly DarkGreen = new SColor("#008000");
+    static readonly Green = new SColor("#00FF00");
+    static readonly DarkCyan = new SColor("#008080");
+    static readonly Cyan = new SColor("#00FFFF");
+    static readonly DarkRed = new SColor("#800000");
+    static readonly Red = new SColor("#FF0000");
+    static readonly DarkMagenta = new SColor("#800080");
+    static readonly Magenta = new SColor("#FF00FF");
+    static readonly DarkYellow = new SColor("#808000");
+    static readonly Yellow = new SColor("#FFFF00");
+    static readonly White = new SColor("#FFFFFF");
+    static readonly DarkGray = new SColor("#808080");
+    static readonly Gray = new SColor("#A0A0A0");
+    static readonly LightGray = new SColor("#C0C0C0");
 
     /**
      * 根据rgb分量生成颜色
@@ -35,7 +35,7 @@ export class SColor {
      */
     static rgb(r: number, g: number, b: number): SColor {
         return new SColor(r, g, b);
-    } // Function rgb()
+    }
 
     /**
      * 根据rgba分量生成颜色
@@ -55,28 +55,56 @@ export class SColor {
     /** 红色分量 */
     get red(): number {
         return (this._value >> 24) & 0xff;
-    } // Get red
+    }
+    set red(v: number) {
+        const r = (Math.floor(v) & 0xff) << 24;
+        this._value = (this._value & 0x00ffffff) | r;
+    }
 
     /** 绿色分量 */
     get green(): number {
         return (this._value >> 16) & 0xff;
-    } // Get green
+    }
+    set green(v: number) {
+        const r = (Math.floor(v) & 0xff) << 16;
+        this._value = (this._value & 0xff00ffff) | r;
+    }
 
     /** 蓝色分量 */
     get blue(): number {
         return (this._value >> 8) & 0xff;
-    } // Get blue
+    }
+    set blue(v: number) {
+        const r = (Math.floor(v) & 0xff) << 8;
+        this._value = (this._value & 0xffff00ff) | r;
+    }
 
     /** 透明度 */
     get alpha(): number {
         return this._value & 0xff;
-    } // Get alpha
+    }
+    set alpha(v: number) {
+        const r = Math.floor(v) & 0xff;
+        this._value = (this._value & 0xffffff00) | r;
+    }
 
     /** 颜色 */
-    private _value: number = 0xff;
+    private _value = 0xff;
     get value(): string {
         return "#" + SStringUtil.num2Hex(this._value, 8);
-    } // Get value
+    }
+    set value(v: string) {
+        if (v.substr(0, 1) != "#") {
+            return;
+        }
+
+        // 先去“#”,再转换16进制数
+        this._value = parseInt(v.substr(1), 16);
+        // 如果未写alpha值,则左移8位+0xff;
+        if (v.length == 7) {
+            this._value = (this._value << 8) + 0xff;
+        }
+    }
 
     ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
     // 构造函数
@@ -135,7 +163,7 @@ export class SColor {
         if (r == undefined) {
             this._value = 0xff;
         } else if (typeof r == "string") {
-            this.setColor(r);
+            this.value = r;
         } else if (r instanceof SColor) {
             this._value = r._value;
         } else if (a == undefined) {
@@ -143,7 +171,7 @@ export class SColor {
         } else {
             this.setRgba(r as number, g as number, b as number, a as number);
         }
-    } // Constructor
+    }
 
     /**
      * 设置颜色
@@ -158,7 +186,7 @@ export class SColor {
             ((g as number) << 16) +
             ((b as number) << 8) +
             0xff;
-    } // Function setRgb()
+    }
 
     /**
      * 设置颜色
@@ -174,23 +202,23 @@ export class SColor {
             ((g as number) << 16) +
             ((b as number) << 8) +
             (a as number);
-    } // Function setRgb()
-
-    /**
-     * 设置颜色
-     *
-     * @param   str     颜色字符串
-     */
-    private setColor(str: string): void {
-        if (str.substr(0, 1) != "#") {
-            return;
-        }
-
-        // 先去“#”,再转换16进制数
-        this._value = parseInt(str.substr(1), 16);
-        // 如果未写alpha值,则左移8位+0xff;
-        if (str.length == 7) {
-            this._value = (this._value << 8) + 0xff;
-        }
-    } // Function setColor()
+    }
+
+    // /**
+    //  * 设置颜色
+    //  *
+    //  * @param   str     颜色字符串
+    //  */
+    // private setColor(str: string): void {
+    //     if (str.substr(0, 1) != "#") {
+    //         return;
+    //     }
+    //
+    //     // 先去“#”,再转换16进制数
+    //     this._value = parseInt(str.substr(1), 16);
+    //     // 如果未写alpha值,则左移8位+0xff;
+    //     if (str.length == 7) {
+    //         this._value = (this._value << 8) + 0xff;
+    //     }
+    // }
 } // Class SColor