import { SStringUtil } from "@saga-web/base/lib"; /** * 颜色类 * * @author 庞利祥(sybotan@126.com) */ export class SColor { 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分量生成颜色 * * @param r 红色分量 * @param g 绿色分量 * @param b 蓝色分量 * @return 颜色 */ static rgb(r: number, g: number, b: number): SColor { return new SColor(r, g, b); } /** * 根据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; } 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; } 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; } set blue(v: number) { const r = (Math.floor(v) & 0xff) << 8; this._value = (this._value & 0xffff00ff) | r; } /** 透明度 */ get alpha(): number { return this._value & 0xff; } set alpha(v: number) { const r = Math.floor(v) & 0xff; this._value = (this._value & 0xffffff00) | r; } /** 颜色 */ private _value = 0xff; get value(): string { return "#" + SStringUtil.num2Hex(this._value, 8); } 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; } } //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // 构造函数 /** * 构造函数 */ 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.value = 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); } } /** * 设置颜色 * * @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; } /** * 设置颜色 * * @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); } // /** // * 设置颜色 // * // * @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