| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196 |
- 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");
- /**
- * 根据rgb分量生成颜色
- *
- * @param r 红色分量
- * @param g 绿色分量
- * @param b 蓝色分量
- * @return 颜色
- */
- static rgb(r: number, g: number, b: number): SColor {
- return new SColor(r, g, b);
- } // Function rgb()
- /**
- * 根据rgba分量生成颜色
- *
- * @param r 红色分量
- * @param g 绿色分量
- * @param b 蓝色分量
- * @param a 透明度分量
- * @return 颜色
- */
- static rgba(r: number, g: number, b: number, a: number): SColor {
- return new SColor(r, g, b, a);
- } // Function rgb()
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- // 属性定义
- /** 红色分量 */
- get red(): number {
- return (this._value >> 24) & 0xff;
- } // Get red
- /** 绿色分量 */
- get green(): number {
- return (this._value >> 16) & 0xff;
- } // Get green
- /** 蓝色分量 */
- get blue(): number {
- return (this._value >> 8) & 0xff;
- } // Get blue
- /** 透明度 */
- get alpha(): number {
- return this._value & 0xff;
- } // Get alpha
- /** 颜色 */
- private _value: number = 0xff;
- get value(): string {
- return "#" + SStringUtil.num2Hex(this._value, 8);
- } // Get value
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- // 构造函数
- /**
- * 构造函数
- */
- constructor();
- /**
- * 构造函数
- *
- * @param r 红色分量
- * @param g 绿色分量
- * @param b 蓝色分量
- */
- constructor(r: number, g: number, b: number);
- /**
- * 构造函数
- *
- * @param r 红色分量
- * @param g 绿色分量
- * @param b 蓝色分量
- * @param a 透明度分量
- */
- constructor(r: number, g: number, b: number, a: number);
- /**
- * 构造函数
- *
- * @param color 颜色
- */
- constructor(color: string);
- /**
- * 构造函数
- *
- * @param color 颜色
- */
- constructor(color: SColor);
- /**
- * 构造函数(重载实现)
- *
- * @param r 红色分量 | 字符串描述颜色 | 颜色
- * @param g 绿色分量
- * @param b 蓝色分量
- * @param a 透明度分量
- */
- constructor(
- r?: number | string | SColor,
- g?: number,
- b?: number,
- a?: number
- ) {
- if (r == undefined) {
- this._value = 0xff;
- } else if (typeof r == "string") {
- this.setColor(r);
- } else if (r instanceof SColor) {
- this._value = r._value;
- } else if (a == undefined) {
- this.setRgb(r as number, g as number, b as number);
- } else {
- this.setRgba(r as number, g as number, b as number, a as number);
- }
- } // Constructor
- /**
- * 设置颜色
- *
- * @param r 红色分量
- * @param g 绿色分量
- * @param b 蓝色分量
- */
- private setRgb(r: number, g: number, b: number): void {
- this._value =
- ((r as number) << 24) +
- ((g as number) << 16) +
- ((b as number) << 8) +
- 0xff;
- } // Function setRgb()
- /**
- * 设置颜色
- *
- * @param r 红色分量
- * @param g 绿色分量
- * @param b 蓝色分量
- * @param a 透明度分量
- */
- private setRgba(r: number, g: number, b: number, a: number): void {
- this._value =
- ((r as number) << 24) +
- ((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()
- } // Class SColor
|