@xort/cga 0.0.79 → 0.0.80

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -16,12 +16,14 @@ export declare class Color extends EventHandler {
16
16
  set a(value: number);
17
17
  set(value: any): this;
18
18
  setHex(hex: number): this;
19
- setStyle(style: string): this | undefined;
19
+ setHexCssString(style: string): this | undefined;
20
20
  setRGB(r: number, g: number, b: number, a?: number): this;
21
21
  clone(): any;
22
22
  copy(color: this): this;
23
23
  equals(c: this): boolean;
24
24
  toArray(array?: number[], offset?: number): number[];
25
- getHex(): number;
26
- toJSON(): number;
25
+ private byteToFloat;
26
+ private floatToByte;
27
+ getHexCssString(): string;
28
+ toJSON(): string;
27
29
  }
@@ -103,20 +103,12 @@ var Color = /** @class */ (function (_super) {
103
103
  };
104
104
  Color.prototype.setHex = function (hex) {
105
105
  hex = Math.floor(hex);
106
- if (hex > 0xffffff) {
107
- this._r = (hex >> 32 & 255) / 255;
108
- this._g = (hex >> 16 & 255) / 255;
109
- this._b = (hex >> 8 & 255) / 255;
110
- this._a = (hex & 255) / 255;
111
- }
112
- else {
113
- this._r = (hex >> 16 & 255) / 255;
114
- this._g = (hex >> 8 & 255) / 255;
115
- this._b = (hex & 255) / 255;
116
- }
106
+ this._r = (hex >> 16 & 255) / 255;
107
+ this._g = (hex >> 8 & 255) / 255;
108
+ this._b = (hex & 255) / 255;
117
109
  return this;
118
110
  };
119
- Color.prototype.setStyle = function (style) {
111
+ Color.prototype.setHexCssString = function (style) {
120
112
  var m = /^\#([A-Fa-f\d]+)$/.exec(style);
121
113
  // hex color
122
114
  var hex = m[1];
@@ -182,11 +174,38 @@ var Color = /** @class */ (function (_super) {
182
174
  array[offset + 3] = this.a;
183
175
  return array;
184
176
  };
185
- Color.prototype.getHex = function () {
186
- return (this.r * 255) << 24 ^ (this.g * 255) << 16 ^ (this.b * 255) << 8 ^ (this.a * 255) << 0;
177
+ Color.prototype.byteToFloat = function (val) {
178
+ return val / 255.0;
179
+ };
180
+ ;
181
+ Color.prototype.floatToByte = function (val) {
182
+ return val === 1.0 ? 255.0 : (val * 256.0) | 0;
183
+ };
184
+ ;
185
+ Color.prototype.getHexCssString = function () {
186
+ var r = this.floatToByte(this._r).toString(16);
187
+ if (r.length < 2) {
188
+ r = "0" + r;
189
+ }
190
+ var g = this.floatToByte(this._g).toString(16);
191
+ if (g.length < 2) {
192
+ g = "0" + g;
193
+ }
194
+ var b = this.floatToByte(this._b).toString(16);
195
+ if (b.length < 2) {
196
+ b = "0" + b;
197
+ }
198
+ if (this._a < 1) {
199
+ var hexAlpha = this.floatToByte(this._a).toString(16);
200
+ if (hexAlpha.length < 2) {
201
+ hexAlpha = "0" + hexAlpha;
202
+ }
203
+ return "#" + r + g + b + hexAlpha;
204
+ }
205
+ return "#" + r + g + b;
187
206
  };
188
207
  Color.prototype.toJSON = function () {
189
- return this.getHex();
208
+ return this.getHexCssString();
190
209
  };
191
210
  return Color;
192
211
  }(eventhandler_1.EventHandler));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xort/cga",
3
- "version": "0.0.79",
3
+ "version": "0.0.80",
4
4
  "description": "Xtor Compute Geometry Algorithm Libary 计算几何算法库",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
package/src/math/Color.ts CHANGED
@@ -5,6 +5,7 @@ export class Color extends EventHandler {
5
5
  _g: number = 0.0;
6
6
  _b: number = 0.0;
7
7
  _a: number = 1.0;
8
+
8
9
  constructor(r: number = 0, g: number = 0, b: number = 0, a: number = 1) {
9
10
  super();
10
11
  this._r = r;
@@ -75,21 +76,15 @@ export class Color extends EventHandler {
75
76
 
76
77
  setHex(hex: number) {
77
78
  hex = Math.floor(hex);
78
- if (hex > 0xffffff) {
79
- this._r = (hex >> 32 & 255) / 255;
80
- this._g = (hex >> 16 & 255) / 255;
81
- this._b = (hex >> 8 & 255) / 255;
82
- this._a = (hex & 255) / 255;
83
- } else {
84
- this._r = (hex >> 16 & 255) / 255;
85
- this._g = (hex >> 8 & 255) / 255;
86
- this._b = (hex & 255) / 255;
87
- }
79
+
80
+ this._r = (hex >> 16 & 255) / 255;
81
+ this._g = (hex >> 8 & 255) / 255;
82
+ this._b = (hex & 255) / 255;
88
83
 
89
84
  return this;
90
85
  }
91
86
 
92
- setStyle(style: string) {
87
+ setHexCssString(style: string) {
93
88
  let m: any = /^\#([A-Fa-f\d]+)$/.exec(style)
94
89
  // hex color
95
90
 
@@ -169,13 +164,39 @@ export class Color extends EventHandler {
169
164
 
170
165
  return array;
171
166
  }
167
+ private byteToFloat(val: number) {
168
+ return val / 255.0;
169
+ };
170
+
171
+ private floatToByte(val: number) {
172
+ return val === 1.0 ? 255.0 : (val * 256.0) | 0;
173
+ };
172
174
 
173
- getHex() {
174
- return (this.r * 255) << 24 ^ (this.g * 255) << 16 ^ (this.b * 255) << 8 ^ (this.a * 255) << 0;
175
+ getHexCssString() {
176
+ let r = this.floatToByte(this._r).toString(16);
177
+ if (r.length < 2) {
178
+ r = `0${r}`;
179
+ }
180
+ let g = this.floatToByte(this._g).toString(16);
181
+ if (g.length < 2) {
182
+ g = `0${g}`;
183
+ }
184
+ let b = this.floatToByte(this._b).toString(16);
185
+ if (b.length < 2) {
186
+ b = `0${b}`;
187
+ }
188
+ if (this._a < 1) {
189
+ let hexAlpha = this.floatToByte(this._a).toString(16);
190
+ if (hexAlpha.length < 2) {
191
+ hexAlpha = `0${hexAlpha}`;
192
+ }
193
+ return `#${r}${g}${b}${hexAlpha}`;
194
+ }
195
+ return `#${r}${g}${b}`;
175
196
  }
176
197
 
177
198
  toJSON() {
178
- return this.getHex();
199
+ return this.getHexCssString();
179
200
  }
180
201
 
181
202
  }