/* * ********************************************************************************************************************* * * !! * .F88X * X8888Y * .}888888N; * i888888N; .:! .I$WI: * R888888I .'N88~ i8}+8Y&8"l8i$8>8W~'>W8}8]KW+8IIN"8& * .R888888I .;N8888~ .X8' "8I.!,/8" !%NY8`"8I8~~8>,88I * +888888N; .8888888Y "&&8Y.}8, * ./888888N; .R888888Y .'}~ .>}'.`+> i}! "i' +/' .'i~ !11,.:">, .~]! .i}i * ~888888%: .I888888l .]88~`1/iY88Ii+1'.R$8$8]"888888888> Y8$ W8E X8E W8888'188Il}Y88$* * 18888888 E8888881 .]W%8$`R8X'&8%++N8i,8N%N8+l8%` .}8N:.R$RE%N88N%N$K$R 188,FE$8%~Y88I * .E888888I .i8888888' .:$8I;88+`E8R:/8N,.>881.`$8E/1/]N8X.Y8N`"KF&&FK!'88*."88K./$88%RN888+~ * 8888888I .,N888888~ ~88i"8W,!N8*.I88.}888%F,i$88"F88" 888:E8X.>88!i88>`888*.}Fl1]*}1YKi' * i888888N' I888Y ]88;/EX*IFKFK88X K8R .l8W 88Y ~88}'88E&%8W.X8N``]88!.$8K .:W8I * .i888888N; I8Y .&8$ .X88! i881.:%888>I88 ;88] +88+.';;;;:.Y88X 18N.,88l .+88/ * .:R888888I * .&888888I Copyright (c) 2009-2020. 博锐尚格科技股份有限公司 * ~8888' * .!88~ All rights reserved. * * ********************************************************************************************************************* */ import { SStringUtil } from "@persagy-web/base"; /** * 颜色类 * * @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; // } // } /** * 颜色转rgb * * */ toRgb(): string { return `rgb(${this.red}, ${this.green}, ${this.blue})`; } // Function toRgb() /** * 颜色转rgba * * */ toRgba(): string { return `rgba(${this.red}, ${this.green}, ${this.blue}, ${this.alpha / 255.0})`; } // Function toRgb() /** * 颜色转6位16进制 * * */ toVal(): string { return "#" + SStringUtil.num2Hex(this._value, 6); } // Function toRgb() } // Class SColor