123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275 |
- /*
- * *********************************************************************************************************************
- *
- * !!
- * .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
|