SColor.ts 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. import { SStringUtil } from "@saga-web/base/lib";
  2. /**
  3. * 颜色类
  4. *
  5. * @author 庞利祥(sybotan@126.com)
  6. */
  7. export class SColor {
  8. static Transparent = new SColor("#00000000");
  9. static Black = new SColor("#000000");
  10. static DarkBlue = new SColor("#000080");
  11. static Blue = new SColor("#0000FF");
  12. static DarkGreen = new SColor("#008000");
  13. static Green = new SColor("#00FF00");
  14. static DarkCyan = new SColor("#008080");
  15. static Cyan = new SColor("#00FFFF");
  16. static DarkRed = new SColor("#800000");
  17. static Red = new SColor("#FF0000");
  18. static DarkMagenta = new SColor("#800080");
  19. static Magenta = new SColor("#FF00FF");
  20. static DarkYellow = new SColor("#808000");
  21. static Yellow = new SColor("#FFFF00");
  22. static White = new SColor("#FFFFFF");
  23. static DarkGray = new SColor("#808080");
  24. static Gray = new SColor("#A0A0A0");
  25. static 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. } // Function rgb()
  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. } // Get red
  55. /** 绿色分量 */
  56. get green(): number {
  57. return (this._value >> 16) & 0xff;
  58. } // Get green
  59. /** 蓝色分量 */
  60. get blue(): number {
  61. return (this._value >> 8) & 0xff;
  62. } // Get blue
  63. /** 透明度 */
  64. get alpha(): number {
  65. return this._value & 0xff;
  66. } // Get alpha
  67. /** 颜色 */
  68. private _value: number = 0xff;
  69. get value(): string {
  70. return "#" + SStringUtil.num2Hex(this._value, 8);
  71. } // Get value
  72. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  73. // 构造函数
  74. /**
  75. * 构造函数
  76. */
  77. constructor();
  78. /**
  79. * 构造函数
  80. *
  81. * @param r 红色分量
  82. * @param g 绿色分量
  83. * @param b 蓝色分量
  84. */
  85. constructor(r: number, g: number, b: number);
  86. /**
  87. * 构造函数
  88. *
  89. * @param r 红色分量
  90. * @param g 绿色分量
  91. * @param b 蓝色分量
  92. * @param a 透明度分量
  93. */
  94. constructor(r: number, g: number, b: number, a: number);
  95. /**
  96. * 构造函数
  97. *
  98. * @param color 颜色
  99. */
  100. constructor(color: string);
  101. /**
  102. * 构造函数
  103. *
  104. * @param color 颜色
  105. */
  106. constructor(color: SColor);
  107. /**
  108. * 构造函数(重载实现)
  109. *
  110. * @param r 红色分量 | 字符串描述颜色 | 颜色
  111. * @param g 绿色分量
  112. * @param b 蓝色分量
  113. * @param a 透明度分量
  114. */
  115. constructor(
  116. r?: number | string | SColor,
  117. g?: number,
  118. b?: number,
  119. a?: number
  120. ) {
  121. if (r == undefined) {
  122. this._value = 0xff;
  123. } else if (typeof r == "string") {
  124. this.setColor(r);
  125. } else if (r instanceof SColor) {
  126. this._value = r._value;
  127. } else if (a == undefined) {
  128. this.setRgb(r as number, g as number, b as number);
  129. } else {
  130. this.setRgba(r as number, g as number, b as number, a as number);
  131. }
  132. } // Constructor
  133. /**
  134. * 设置颜色
  135. *
  136. * @param r 红色分量
  137. * @param g 绿色分量
  138. * @param b 蓝色分量
  139. */
  140. private setRgb(r: number, g: number, b: number): void {
  141. this._value =
  142. ((r as number) << 24) +
  143. ((g as number) << 16) +
  144. ((b as number) << 8) +
  145. 0xff;
  146. } // Function setRgb()
  147. /**
  148. * 设置颜色
  149. *
  150. * @param r 红色分量
  151. * @param g 绿色分量
  152. * @param b 蓝色分量
  153. * @param a 透明度分量
  154. */
  155. private setRgba(r: number, g: number, b: number, a: number): void {
  156. this._value =
  157. ((r as number) << 24) +
  158. ((g as number) << 16) +
  159. ((b as number) << 8) +
  160. (a as number);
  161. } // Function setRgb()
  162. /**
  163. * 设置颜色
  164. *
  165. * @param str 颜色字符串
  166. */
  167. private setColor(str: string): void {
  168. if (str.substr(0, 1) != "#") {
  169. return;
  170. }
  171. // 先去“#”,再转换16进制数
  172. this._value = parseInt(str.substr(1), 16);
  173. // 如果未写alpha值,则左移8位+0xff;
  174. if (str.length == 7) {
  175. this._value = (this._value << 8) + 0xff;
  176. }
  177. } // Function setColor()
  178. } // Class SColor