hw_22_05_rgbA.html 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <header>RGBA</header>
  2. <body>
  3. <script>
  4. class RGB {
  5. #r = 0
  6. #g = 0
  7. #b = 0
  8. onError(message) {
  9. //throw new Error(message);
  10. console.log(`ERROR: ${message}`);
  11. }
  12. toHex(val) {
  13. return val.toString(16).padStart(2, '0').toUpperCase();
  14. }
  15. checkChannelValue(val) {
  16. val = +val
  17. if (typeof val !== "number" || val < 0 || val > 255) {
  18. this.onError(`invalid channel value: ${val}`)
  19. }
  20. }
  21. get r() {
  22. return this.#r;
  23. }
  24. set r(val) {
  25. this.checkChannelValue(val);
  26. this.#r = +val;
  27. }
  28. get g() {
  29. return this.#g;
  30. }
  31. set g(val) {
  32. this.checkChannelValue(val);
  33. this.#g = +val;
  34. }
  35. get b() {
  36. return this.#b;
  37. }
  38. set b(val) {
  39. this.checkChannelValue(val);
  40. this.#b = +val;
  41. }
  42. set hex(val) {
  43. let matchColors = /^#([0-9A-Fa-f]{2})([0-9A-Fa-f]{2})([0-9A-Fa-f]{2})$/;
  44. let match = matchColors.exec(val);
  45. if (match !== null && match.length == 4) {
  46. this.r = parseInt(`${match[1]}`, 16);
  47. this.g = parseInt(`${match[2]}`, 16);
  48. this.b = parseInt(`${match[3]}`, 16);
  49. return;
  50. }
  51. this.onError(`incorrect hex color parameter: ${val}`);
  52. }
  53. get hex() {
  54. return `#${this.toHex(this.r)}${this.toHex(this.g)}${this.toHex(this.b)}`;
  55. }
  56. set color(val) {
  57. let matchColors = /^rgb\((\d{1,3}), (\d{1,3}), (\d{1,3})\)$/;
  58. let match = matchColors.exec(val);
  59. if (match !== null && match.length == 4) {
  60. this.r = match[1];
  61. this.g = match[2];
  62. this.b = match[3];
  63. return;
  64. }
  65. this.onError(`incorrect rgb color parameter: ${val}`);
  66. }
  67. get color() {
  68. return `rgb(${this.r}, ${this.g}, ${this.b})`;
  69. }
  70. constructor(colorString) {
  71. if (colorString) {
  72. if (colorString.startsWith(`#`))
  73. this.hex = colorString;
  74. else
  75. this.color = colorString;
  76. }
  77. }
  78. }
  79. function assertAreEqual(actual, expected) {
  80. if (actual != expected)
  81. throw new Error(`not expected value. actual: ${actual}, expected: ${expected}`);
  82. }
  83. //////////////////////////////////////////////////////////////////////
  84. class RGBA extends RGB {
  85. #a = 0;
  86. get a() {
  87. return this.#a;
  88. }
  89. set a(val) {
  90. this.checkChannelValue(val * 255);
  91. this.#a = +val;
  92. }
  93. set color(val) {
  94. if (val.startsWith('rgb(')) {
  95. super.color = val;
  96. this.#a = 0;
  97. return;
  98. }
  99. else {
  100. let matchColors = /^rgba\((\d{1,3}), *(\d{1,3}), *(\d{1,3})(?:, *(\d*\.?\d+))?\)$/;
  101. let match = matchColors.exec(val);
  102. if (match !== null && match.length == 5) {
  103. this.r = match[1];
  104. this.g = match[2];
  105. this.b = match[3];
  106. this.a = match[4];
  107. return;
  108. }
  109. }
  110. this.onError(`incorrect rgba color parameter: ${val}`);
  111. }
  112. get color() {
  113. return `rgba(${this.r}, ${this.g}, ${this.b}, ${this.a.toFixed(3)})`;
  114. }
  115. set hex(val) {
  116. let matchColors = /^#([0-9A-Fa-f]{2})([0-9A-Fa-f]{2})([0-9A-Fa-f]{2})([0-9A-Fa-f]{2})?$/;
  117. let match = matchColors.exec(val);
  118. if (match !== null && match.length == 5) {
  119. if (!match[4]) {
  120. super.hex = val;
  121. this.#a = 0;
  122. }
  123. else {
  124. super.hex = `#${match[1]}${match[2]}${match[3]}`;
  125. this.a = parseInt(`${match[4]}`, 16) / 255;
  126. }
  127. return;
  128. }
  129. this.onError(`incorrect hex color parameter: ${val}`);
  130. }
  131. get hex() {
  132. return `${super.hex}${this.toHex(Math.round(this.a * 255))}`;
  133. }
  134. }
  135. const rgba = new RGBA
  136. rgba.hex = '#80808080'
  137. console.log(rgba.a) //0.5
  138. console.log(rgba.color) //rgba(128,128,128,0.5)
  139. rgba.hex = '#808080'
  140. console.log(rgba.color) //rgba(128,128,128,0.000)
  141. rgba.r = 192
  142. rgba.a = 0.25
  143. console.log(rgba.hex) //#C0808040
  144. rgba.color = 'rgba(1, 2, 3, 0.70)'
  145. rgba.b *= 10
  146. console.log(rgba.hex) //#01021EB3
  147. rgba.color = 'rgb(1, 2, 3)'
  148. console.log(rgba.hex)
  149. </script>
  150. </body>