SColor.ts 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. import { SStringUtil } from "@saga-web/base/lib";
  2. /**
  3. * 颜色类
  4. *
  5. * @author 庞利祥(sybotan@126.com)
  6. */
  7. export class SColor {
  8. static readonly Transparent = new SColor("#00000000");
  9. static readonly Black = new SColor("#000000");
  10. static readonly DarkBlue = new SColor("#000080");
  11. static readonly Blue = new SColor("#0000FF");
  12. static readonly DarkGreen = new SColor("#008000");
  13. static readonly Green = new SColor("#00FF00");
  14. static readonly DarkCyan = new SColor("#008080");
  15. static readonly Cyan = new SColor("#00FFFF");
  16. static readonly DarkRed = new SColor("#800000");
  17. static readonly Red = new SColor("#FF0000");
  18. static readonly DarkMagenta = new SColor("#800080");
  19. static readonly Magenta = new SColor("#FF00FF");
  20. static readonly DarkYellow = new SColor("#808000");
  21. static readonly Yellow = new SColor("#FFFF00");
  22. static readonly White = new SColor("#FFFFFF");
  23. static readonly DarkGray = new SColor("#808080");
  24. static readonly Gray = new SColor("#A0A0A0");
  25. static readonly LightGray = new SColor("#C0C0C0");
  26. /**
  27. * 根据rgb分量生成颜色
  28. *
  29. * @param r 红色分量
  30. * @param g 绿色分量
  31. * @param b 蓝色分量
  32. * @return 颜色
  33. */
  34. static rgb(r: number, g: number, b: number): SColor {
  35. return new SColor(r, g, b);
  36. }
  37. /**
  38. * 根据rgba分量生成颜色
  39. *
  40. * @param r 红色分量
  41. * @param g 绿色分量
  42. * @param b 蓝色分量
  43. * @param a 透明度分量
  44. * @return 颜色
  45. */
  46. static rgba(r: number, g: number, b: number, a: number): SColor {
  47. return new SColor(r, g, b, a);
  48. } // Function rgb()
  49. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  50. // 属性定义
  51. /** 红色分量 */
  52. get red(): number {
  53. return (this._value >> 24) & 0xff;
  54. }
  55. set red(v: number) {
  56. const r = (Math.floor(v) & 0xff) << 24;
  57. this._value = (this._value & 0x00ffffff) | r;
  58. }
  59. /** 绿色分量 */
  60. get green(): number {
  61. return (this._value >> 16) & 0xff;
  62. }
  63. set green(v: number) {
  64. const r = (Math.floor(v) & 0xff) << 16;
  65. this._value = (this._value & 0xff00ffff) | r;
  66. }
  67. /** 蓝色分量 */
  68. get blue(): number {
  69. return (this._value >> 8) & 0xff;
  70. }
  71. set blue(v: number) {
  72. const r = (Math.floor(v) & 0xff) << 8;
  73. this._value = (this._value & 0xffff00ff) | r;
  74. }
  75. /** 透明度 */
  76. get alpha(): number {
  77. return this._value & 0xff;
  78. }
  79. set alpha(v: number) {
  80. const r = Math.floor(v) & 0xff;
  81. this._value = (this._value & 0xffffff00) | r;
  82. }
  83. /** 颜色 */
  84. private _value = 0xff;
  85. get value(): string {
  86. return "#" + SStringUtil.num2Hex(this._value, 8);
  87. }
  88. set value(v: string) {
  89. if (v.substr(0, 1) != "#") {
  90. return;
  91. }
  92. // 先去“#”,再转换16进制数
  93. this._value = parseInt(v.substr(1), 16);
  94. // 如果未写alpha值,则左移8位+0xff;
  95. if (v.length == 7) {
  96. this._value = (this._value << 8) + 0xff;
  97. }
  98. }
  99. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  100. // 构造函数
  101. /**
  102. * 构造函数
  103. */
  104. constructor();
  105. /**
  106. * 构造函数
  107. *
  108. * @param r 红色分量
  109. * @param g 绿色分量
  110. * @param b 蓝色分量
  111. */
  112. constructor(r: number, g: number, b: number);
  113. /**
  114. * 构造函数
  115. *
  116. * @param r 红色分量
  117. * @param g 绿色分量
  118. * @param b 蓝色分量
  119. * @param a 透明度分量
  120. */
  121. constructor(r: number, g: number, b: number, a: number);
  122. /**
  123. * 构造函数
  124. *
  125. * @param color 颜色
  126. */
  127. constructor(color: string);
  128. /**
  129. * 构造函数
  130. *
  131. * @param color 颜色
  132. */
  133. constructor(color: SColor);
  134. /**
  135. * 构造函数(重载实现)
  136. *
  137. * @param r 红色分量 | 字符串描述颜色 | 颜色
  138. * @param g 绿色分量
  139. * @param b 蓝色分量
  140. * @param a 透明度分量
  141. */
  142. constructor(
  143. r?: number | string | SColor,
  144. g?: number,
  145. b?: number,
  146. a?: number
  147. ) {
  148. if (r == undefined) {
  149. this._value = 0xff;
  150. } else if (typeof r == "string") {
  151. this.value = r;
  152. } else if (r instanceof SColor) {
  153. this._value = r._value;
  154. } else if (a == undefined) {
  155. this.setRgb(r as number, g as number, b as number);
  156. } else {
  157. this.setRgba(r as number, g as number, b as number, a as number);
  158. }
  159. }
  160. /**
  161. * 设置颜色
  162. *
  163. * @param r 红色分量
  164. * @param g 绿色分量
  165. * @param b 蓝色分量
  166. */
  167. private setRgb(r: number, g: number, b: number): void {
  168. this._value =
  169. ((r as number) << 24) +
  170. ((g as number) << 16) +
  171. ((b as number) << 8) +
  172. 0xff;
  173. }
  174. /**
  175. * 设置颜色
  176. *
  177. * @param r 红色分量
  178. * @param g 绿色分量
  179. * @param b 蓝色分量
  180. * @param a 透明度分量
  181. */
  182. private setRgba(r: number, g: number, b: number, a: number): void {
  183. this._value =
  184. ((r as number) << 24) +
  185. ((g as number) << 16) +
  186. ((b as number) << 8) +
  187. (a as number);
  188. }
  189. // /**
  190. // * 设置颜色
  191. // *
  192. // * @param str 颜色字符串
  193. // */
  194. // private setColor(str: string): void {
  195. // if (str.substr(0, 1) != "#") {
  196. // return;
  197. // }
  198. //
  199. // // 先去“#”,再转换16进制数
  200. // this._value = parseInt(str.substr(1), 16);
  201. // // 如果未写alpha值,则左移8位+0xff;
  202. // if (str.length == 7) {
  203. // this._value = (this._value << 8) + 0xff;
  204. // }
  205. // }
  206. } // Class SColor