SColor.ts 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /*
  2. * *********************************************************************************************************************
  3. *
  4. * !!
  5. * .F88X
  6. * X8888Y
  7. * .}888888N;
  8. * i888888N; .:! .I$WI:
  9. * R888888I .'N88~ i8}+8Y&8"l8i$8>8W~'>W8}8]KW+8IIN"8&
  10. * .R888888I .;N8888~ .X8' "8I.!,/8" !%NY8`"8I8~~8>,88I
  11. * +888888N; .8888888Y "&&8Y.}8,
  12. * ./888888N; .R888888Y .'}~ .>}'.`+> i}! "i' +/' .'i~ !11,.:">, .~]! .i}i
  13. * ~888888%: .I888888l .]88~`1/iY88Ii+1'.R$8$8]"888888888> Y8$ W8E X8E W8888'188Il}Y88$*
  14. * 18888888 E8888881 .]W%8$`R8X'&8%++N8i,8N%N8+l8%` .}8N:.R$RE%N88N%N$K$R 188,FE$8%~Y88I
  15. * .E888888I .i8888888' .:$8I;88+`E8R:/8N,.>881.`$8E/1/]N8X.Y8N`"KF&&FK!'88*."88K./$88%RN888+~
  16. * 8888888I .,N888888~ ~88i"8W,!N8*.I88.}888%F,i$88"F88" 888:E8X.>88!i88>`888*.}Fl1]*}1YKi'
  17. * i888888N' I888Y ]88;/EX*IFKFK88X K8R .l8W 88Y ~88}'88E&%8W.X8N``]88!.$8K .:W8I
  18. * .i888888N; I8Y .&8$ .X88! i881.:%888>I88 ;88] +88+.';;;;:.Y88X 18N.,88l .+88/
  19. * .:R888888I
  20. * .&888888I Copyright (c) 2009-2020. 博锐尚格科技股份有限公司
  21. * ~8888'
  22. * .!88~ All rights reserved.
  23. *
  24. * *********************************************************************************************************************
  25. */
  26. import { SStringUtil } from "@persagy-web/base";
  27. /**
  28. * 颜色类
  29. *
  30. * @author 庞利祥 <sybotan@126.com>
  31. */
  32. export class SColor {
  33. /** 透明色 */
  34. static readonly Transparent = new SColor("#00000000");
  35. /** 黑色 */
  36. static readonly Black = new SColor("#000000");
  37. /** 深蓝色 */
  38. static readonly DarkBlue = new SColor("#000080");
  39. /** 蓝色 */
  40. static readonly Blue = new SColor("#0000FF");
  41. /** 深绿色 */
  42. static readonly DarkGreen = new SColor("#008000");
  43. /** 绿色 */
  44. static readonly Green = new SColor("#00FF00");
  45. /** 深青色 */
  46. static readonly DarkCyan = new SColor("#008080");
  47. /** 青色 */
  48. static readonly Cyan = new SColor("#00FFFF");
  49. /** 深红色 */
  50. static readonly DarkRed = new SColor("#800000");
  51. /** 红色 */
  52. static readonly Red = new SColor("#FF0000");
  53. /** 深洋红色 */
  54. static readonly DarkMagenta = new SColor("#800080");
  55. /** 洋红色 */
  56. static readonly Magenta = new SColor("#FF00FF");
  57. /** 深黄色 */
  58. static readonly DarkYellow = new SColor("#808000");
  59. /** 黄色 */
  60. static readonly Yellow = new SColor("#FFFF00");
  61. /** 白色 */
  62. static readonly White = new SColor("#FFFFFF");
  63. /** 深灰色 */
  64. static readonly DarkGray = new SColor("#808080");
  65. /** 灰色 */
  66. static readonly Gray = new SColor("#A0A0A0");
  67. /** 浅灰色 */
  68. static readonly LightGray = new SColor("#C0C0C0");
  69. /**
  70. * 根据 rgb 分量生成颜色
  71. *
  72. * @param r 红色分量
  73. * @param g 绿色分量
  74. * @param b 蓝色分量
  75. * @return 颜色
  76. */
  77. static rgb(r: number, g: number, b: number): SColor {
  78. return new SColor(r, g, b);
  79. }
  80. /**
  81. * 根据 rgba 分量生成颜色
  82. *
  83. * @param r 红色分量
  84. * @param g 绿色分量
  85. * @param b 蓝色分量
  86. * @param a 透明度分量
  87. * @return 颜色
  88. */
  89. static rgba(r: number, g: number, b: number, a: number): SColor {
  90. return new SColor(r, g, b, a);
  91. }
  92. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  93. // 属性定义
  94. /** 红色分量 */
  95. get red(): number {
  96. return (this._value >> 24) & 0xff;
  97. }
  98. set red(v: number) {
  99. const r = (Math.floor(v) & 0xff) << 24;
  100. this._value = (this._value & 0x00ffffff) | r;
  101. }
  102. /** 绿色分量 */
  103. get green(): number {
  104. return (this._value >> 16) & 0xff;
  105. }
  106. set green(v: number) {
  107. const r = (Math.floor(v) & 0xff) << 16;
  108. this._value = (this._value & 0xff00ffff) | r;
  109. }
  110. /** 蓝色分量 */
  111. get blue(): number {
  112. return (this._value >> 8) & 0xff;
  113. }
  114. set blue(v: number) {
  115. const r = (Math.floor(v) & 0xff) << 8;
  116. this._value = (this._value & 0xffff00ff) | r;
  117. }
  118. /** 透明度 */
  119. get alpha(): number {
  120. return this._value & 0xff;
  121. }
  122. set alpha(v: number) {
  123. const r = Math.floor(v) & 0xff;
  124. this._value = (this._value & 0xffffff00) | r;
  125. }
  126. /** 颜色 */
  127. private _value = 0xff;
  128. get value(): string {
  129. return "#" + SStringUtil.num2Hex(this._value, 8);
  130. }
  131. set value(v: string) {
  132. if (v.substr(0, 1) != "#") {
  133. return;
  134. }
  135. // 先去“#”,再转换16进制数
  136. this._value = parseInt(v.substr(1), 16);
  137. // 如果未写alpha值,则左移8位+0xff;
  138. if (v.length == 7) {
  139. this._value = (this._value << 8) + 0xff;
  140. }
  141. }
  142. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  143. // 构造函数
  144. /**
  145. * 构造函数
  146. */
  147. constructor();
  148. /**
  149. * 构造函数
  150. *
  151. * @param r 红色分量
  152. * @param g 绿色分量
  153. * @param b 蓝色分量
  154. */
  155. constructor(r: number, g: number, b: number);
  156. /**
  157. * 构造函数
  158. *
  159. * @param r 红色分量
  160. * @param g 绿色分量
  161. * @param b 蓝色分量
  162. * @param a 透明度分量 0 - 255
  163. */
  164. constructor(r: number, g: number, b: number, a: number);
  165. /**
  166. * 构造函数
  167. *
  168. * @param color 颜色
  169. */
  170. constructor(color: string);
  171. /**
  172. * 构造函数
  173. *
  174. * @param color 颜色
  175. */
  176. constructor(color: SColor);
  177. /**
  178. * 构造函数(重载实现)
  179. *
  180. * @param r 红色分量 | 字符串描述颜色 | 颜色
  181. * @param g 绿色分量
  182. * @param b 蓝色分量
  183. * @param a 透明度分量 0 - 255
  184. */
  185. constructor(
  186. r?: number | string | SColor,
  187. g?: number,
  188. b?: number,
  189. a?: number
  190. ) {
  191. if (r == undefined) {
  192. this._value = 0xff;
  193. } else if (typeof r == "string") {
  194. this.value = r;
  195. } else if (r instanceof SColor) {
  196. this._value = r._value;
  197. } else if (a == undefined) {
  198. this.setRgb(r as number, g as number, b as number);
  199. } else {
  200. this.setRgba(r as number, g as number, b as number, a as number);
  201. }
  202. }
  203. /**
  204. * 设置颜色
  205. *
  206. * @param r 红色分量
  207. * @param g 绿色分量
  208. * @param b 蓝色分量
  209. */
  210. private setRgb(r: number, g: number, b: number): void {
  211. this._value =
  212. ((r as number) << 24) +
  213. ((g as number) << 16) +
  214. ((b as number) << 8) +
  215. 0xff;
  216. }
  217. /**
  218. * 设置颜色
  219. *
  220. * @param r 红色分量
  221. * @param g 绿色分量
  222. * @param b 蓝色分量
  223. * @param a 透明度分量 0 - 255
  224. */
  225. private setRgba(r: number, g: number, b: number, a: number): void {
  226. this._value =
  227. ((r as number) << 24) +
  228. ((g as number) << 16) +
  229. ((b as number) << 8) +
  230. (a as number);
  231. }
  232. /**
  233. * 颜色转 rgb
  234. *
  235. * @return rgb 字符串
  236. */
  237. toRgb(): string {
  238. return `rgb(${this.red}, ${this.green}, ${this.blue})`;
  239. }
  240. /**
  241. * 颜色转 rgba
  242. *
  243. * @return rgba 字符串
  244. */
  245. toRgba(): string {
  246. return `rgba(${this.red}, ${this.green}, ${this.blue}, ${this.alpha /
  247. 255.0})`;
  248. }
  249. /**
  250. * 颜色转 6 位 16 进制
  251. *
  252. * @return 6 位 16 进制颜色
  253. */
  254. toVal(): string {
  255. return "#" + SStringUtil.num2Hex(this._value, 6);
  256. }
  257. }