@stocksharp/diagram 1.2.0 → 1.2.2

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.
package/src/embed.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import { t } from './i18n.js';
2
+ import { luminance } from './color.js';
2
3
  // Shared read-only diagram embed layer. Rendering, palette loading and theming
3
4
  // live next to the diagram engine so web applications do not copy that logic.
4
5
  //
@@ -81,62 +82,6 @@ function iconUrl(name: string): string {
81
82
 
82
83
  // Perceived luminance (0..255) of a #rrggbb colour; used to tell a light diagram canvas from a dark
83
84
  // one so the (dark-canvas-tuned) link palette can be darkened when the canvas is light.
84
- const CSS_COLOR_KEYWORDS: Readonly<Record<string, [number, number, number]>> = {
85
- white: [255, 255, 255],
86
- black: [0, 0, 0],
87
- transparent: [255, 255, 255],
88
- };
89
-
90
- /**
91
- * Perceived brightness of a CSS colour, or -1 when it cannot be read.
92
- *
93
- * Six-digit hex was the only form understood, and everything else answered 0 -- black. A page
94
- * writing --diagram-bg as #fff, white or rgb(255 255 255) therefore had its white canvas
95
- * classified as dark and got the dark link palette: near-white wires on a near-white background.
96
- * Unreadable is now -1 rather than 0, so the caller can keep its own default instead of being
97
- * told the page is black.
98
- */
99
- function luminance(color: string): number {
100
- const value = color.trim().toLowerCase();
101
- if (value === '')
102
- return -1;
103
-
104
- const keyword = CSS_COLOR_KEYWORDS[value];
105
- if (keyword !== undefined)
106
- return 0.299 * keyword[0] + 0.587 * keyword[1] + 0.114 * keyword[2];
107
-
108
- const long = value.match(/^#?([0-9a-f]{6})$/);
109
- if (long !== null) {
110
- const n = parseInt(long[1], 16);
111
- return 0.299 * ((n >> 16) & 255) + 0.587 * ((n >> 8) & 255) + 0.114 * (n & 255);
112
- }
113
-
114
- // #abc is shorthand for #aabbcc, and the eight-digit forms carry an alpha we ignore.
115
- const short = value.match(/^#?([0-9a-f]{3})([0-9a-f])?$/);
116
- if (short !== null) {
117
- const [r, g, b] = [...short[1]].map((digit) => parseInt(digit + digit, 16));
118
- return 0.299 * r + 0.587 * g + 0.114 * b;
119
- }
120
- const long8 = value.match(/^#?([0-9a-f]{6})[0-9a-f]{2}$/);
121
- if (long8 !== null) {
122
- const n = parseInt(long8[1], 16);
123
- return 0.299 * ((n >> 16) & 255) + 0.587 * ((n >> 8) & 255) + 0.114 * (n & 255);
124
- }
125
-
126
- // rgb()/rgba(), both the comma and the space syntax.
127
- const rgb = value.match(/^rgba?\(([^)]+)\)$/);
128
- if (rgb !== null) {
129
- const parts = rgb[1].split(/[\s,/]+/).filter((part) => part !== '');
130
- if (parts.length >= 3) {
131
- const [r, g, b] = parts.slice(0, 3).map((part) => part.endsWith('%')
132
- ? (parseFloat(part) / 100) * 255
133
- : parseFloat(part));
134
- if ([r, g, b].every((channel) => Number.isFinite(channel)))
135
- return 0.299 * r + 0.587 * g + 0.114 * b;
136
- }
137
- }
138
- return -1;
139
- }
140
85
 
141
86
  function makePort(p: PalettePort): Port {
142
87
  return new Port({