@fbltd/math 1.0.44 → 1.0.46

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.
@@ -1,5 +1,10 @@
1
1
  import { validateType } from "../type.utils.js";
2
2
  import { denormalizeShade } from "./utils.js";
3
+ export var ColorStringFormat;
4
+ (function (ColorStringFormat) {
5
+ ColorStringFormat["RGBA"] = "rgba";
6
+ ColorStringFormat["HEX"] = "hex";
7
+ })(ColorStringFormat || (ColorStringFormat = {}));
3
8
  export class Color {
4
9
  _red;
5
10
  _green;
@@ -42,7 +47,7 @@ export class Color {
42
47
  [Symbol.toPrimitive]() {
43
48
  return this.toString();
44
49
  }
45
- toString(format = 'hex') {
50
+ toString(format = ColorStringFormat.HEX) {
46
51
  return Color.representation(this, format);
47
52
  }
48
53
  toNumber() {
@@ -63,14 +68,20 @@ export class Color {
63
68
  static areEquals(c1, c2) {
64
69
  return Color.toNumber(c1) === Color.toNumber(c2);
65
70
  }
66
- static representation(color, format = 'hex') {
71
+ static representation(color, format = ColorStringFormat.HEX) {
67
72
  switch (format) {
68
- case "rgba":
73
+ case ColorStringFormat.RGBA:
69
74
  return `rgba(${color.red},${color.green},${color.blue},${color.alpha})`;
70
- case "hex":
71
- return `#${color.red.toString(16)}${color.green.toString(16)}${color.blue.toString(16)}${denormalizeShade(color.alpha).toString(16)}`;
75
+ case ColorStringFormat.HEX:
76
+ return `#${this.shadeToHexDigitsDenormalized(color.red)}${this.shadeToHexDigitsDenormalized(color.green)}${this.shadeToHexDigitsDenormalized(color.blue)}${this.shadeToHexDigitsNormalized(color.alpha)}`;
72
77
  default:
73
78
  validateType(format);
74
79
  }
75
80
  }
81
+ static shadeToHexDigitsNormalized(shade) {
82
+ return this.shadeToHexDigitsDenormalized(denormalizeShade(shade));
83
+ }
84
+ static shadeToHexDigitsDenormalized(shade) {
85
+ return shade.toString(16).padStart(2, '0');
86
+ }
76
87
  }
@@ -3,5 +3,5 @@ export function normalizeShade(value) {
3
3
  return normalize(value, 255);
4
4
  }
5
5
  export function denormalizeShade(value) {
6
- return denormalize(value, 255);
6
+ return Math.floor(denormalize(value, 255));
7
7
  }
@@ -1,3 +1,7 @@
1
+ export declare enum ColorStringFormat {
2
+ RGBA = "rgba",
3
+ HEX = "hex"
4
+ }
1
5
  export declare class Color {
2
6
  private _red;
3
7
  private _green;
@@ -20,5 +24,7 @@ export declare class Color {
20
24
  static ofNumberAlpha(value: number): Color;
21
25
  static toNumber(color: Color): number;
22
26
  static areEquals(c1: Color, c2: Color): boolean;
23
- static representation(color: Color, format?: 'rgba' | 'hex'): string;
27
+ static representation(color: Color, format?: ColorStringFormat): string;
28
+ static shadeToHexDigitsNormalized(shade: number): string;
29
+ static shadeToHexDigitsDenormalized(shade: number): string;
24
30
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fbltd/math",
3
- "version": "1.0.44",
3
+ "version": "1.0.46",
4
4
  "description": "Math and geometry utilities",
5
5
  "sideEffects": false,
6
6
  "main": "dist/bin/index.js",