@guilhermebkel/scenec 0.2.0 → 0.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/README.md +23 -12
- package/dist/Adapters/FontAdapter.js +5 -1
- package/dist/Commands/BestCommand.d.ts +11 -0
- package/dist/Commands/BestCommand.js +73 -0
- package/dist/Commands/RenderCommand.d.ts +2 -0
- package/dist/Commands/RenderCommand.js +7 -0
- package/dist/Modules/BestModule.d.ts +41 -0
- package/dist/Modules/BestModule.js +159 -0
- package/dist/Modules/CompilationModule.d.ts +5 -0
- package/dist/Modules/CompilationModule.js +92 -19
- package/dist/Modules/CritiqueModule.d.ts +3 -0
- package/dist/Modules/CritiqueModule.js +60 -5
- package/dist/Protocols/AssetProtocol.d.ts +6 -1
- package/dist/Protocols/AssetProtocol.js +17 -1
- package/dist/Protocols/CommandProtocol.d.ts +2 -0
- package/dist/Protocols/CompilationProtocol.d.ts +27 -1
- package/dist/Protocols/CritiqueProtocol.d.ts +17 -1
- package/dist/Protocols/CritiqueProtocol.js +11 -1
- package/dist/Protocols/KitProtocol.d.ts +8 -2
- package/dist/Protocols/KitProtocol.js +8 -2
- package/dist/Protocols/PersonProtocol.d.ts +4 -0
- package/dist/Protocols/PersonProtocol.js +2 -0
- package/dist/Protocols/SceneProtocol.d.ts +204 -5
- package/dist/Protocols/SceneProtocol.js +60 -7
- package/dist/Resources/Assets/BaseAsset.d.ts +1 -0
- package/dist/Resources/Assets/BaseAsset.js +1 -0
- package/dist/Resources/Assets/BigPlantAsset.js +8 -1
- package/dist/Resources/Assets/BlackboardAsset.d.ts +11 -0
- package/dist/Resources/Assets/BlackboardAsset.js +20 -0
- package/dist/Resources/Assets/BushAsset.js +1 -1
- package/dist/Resources/Assets/ChairAsset.d.ts +5 -0
- package/dist/Resources/Assets/ChairAsset.js +23 -0
- package/dist/Resources/Assets/ChatBubbleAsset.d.ts +11 -0
- package/dist/Resources/Assets/ChatBubbleAsset.js +18 -0
- package/dist/Resources/Assets/CloudAsset.js +3 -1
- package/dist/Resources/Assets/ConfettiAsset.d.ts +10 -0
- package/dist/Resources/Assets/ConfettiAsset.js +18 -0
- package/dist/Resources/Assets/CursorAsset.d.ts +11 -0
- package/dist/Resources/Assets/CursorAsset.js +17 -0
- package/dist/Resources/Assets/DeskAsset.js +7 -2
- package/dist/Resources/Assets/FirstAidKitAsset.d.ts +10 -0
- package/dist/Resources/Assets/FirstAidKitAsset.js +19 -0
- package/dist/Resources/Assets/GlobeAsset.d.ts +10 -0
- package/dist/Resources/Assets/GlobeAsset.js +18 -0
- package/dist/Resources/Assets/HouseAsset.js +22 -6
- package/dist/Resources/Assets/LaptopAsset.js +5 -1
- package/dist/Resources/Assets/PlantAsset.js +1 -1
- package/dist/Resources/Assets/PodiumAsset.d.ts +11 -0
- package/dist/Resources/Assets/PodiumAsset.js +20 -0
- package/dist/Resources/Assets/RefrigeratorAsset.d.ts +11 -0
- package/dist/Resources/Assets/RefrigeratorAsset.js +21 -0
- package/dist/Resources/Assets/SinkAsset.d.ts +11 -0
- package/dist/Resources/Assets/SinkAsset.js +21 -0
- package/dist/Resources/Assets/SofaAsset.d.ts +5 -0
- package/dist/Resources/Assets/SofaAsset.js +30 -2
- package/dist/Resources/Assets/StarsAsset.d.ts +6 -0
- package/dist/Resources/Assets/StarsAsset.js +37 -24
- package/dist/Resources/Assets/StethoscopeAsset.d.ts +10 -0
- package/dist/Resources/Assets/StethoscopeAsset.js +18 -0
- package/dist/Resources/Assets/StoveAsset.d.ts +11 -0
- package/dist/Resources/Assets/StoveAsset.js +20 -0
- package/dist/Resources/Assets/ToggleAsset.d.ts +11 -0
- package/dist/Resources/Assets/ToggleAsset.js +21 -0
- package/dist/Resources/Assets/TreeAsset.d.ts +5 -0
- package/dist/Resources/Assets/TreeAsset.js +32 -7
- package/dist/Resources/Assets/WaterCoolerAsset.d.ts +11 -0
- package/dist/Resources/Assets/WaterCoolerAsset.js +20 -0
- package/dist/Resources/Assets/WhiteboardAsset.d.ts +10 -0
- package/dist/Resources/Assets/WhiteboardAsset.js +18 -0
- package/dist/Resources/Kits/BaseKit.d.ts +2 -1
- package/dist/Resources/Kits/BaseKit.js +1 -0
- package/dist/Resources/Kits/ClayKit.d.ts +9 -0
- package/dist/Resources/Kits/ClayKit.js +11 -0
- package/dist/Resources/Kits/DuotoneKit.d.ts +16 -0
- package/dist/Resources/Kits/DuotoneKit.js +18 -0
- package/dist/Resources/Kits/IsometricKit.d.ts +8 -0
- package/dist/Resources/Kits/IsometricKit.js +10 -0
- package/dist/Resources/Kits/OutlineKit.d.ts +3 -0
- package/dist/Resources/Kits/OutlineKit.js +1 -1
- package/dist/Resources/Kits/PaperKit.d.ts +2 -0
- package/dist/Resources/Kits/PaperKit.js +1 -0
- package/dist/Resources/Kits/PosterKit.d.ts +14 -0
- package/dist/Resources/Kits/PosterKit.js +16 -0
- package/dist/Resources/Kits/SketchKit.d.ts +2 -1
- package/dist/Resources/Kits/SketchKit.js +1 -0
- package/dist/Services/AssetService.js +29 -1
- package/dist/Services/BackgroundService.d.ts +10 -2
- package/dist/Services/BackgroundService.js +52 -11
- package/dist/Services/CatalogService.d.ts +1 -0
- package/dist/Services/CatalogService.js +8 -3
- package/dist/Services/ColorCritiqueService.d.ts +42 -0
- package/dist/Services/ColorCritiqueService.js +232 -0
- package/dist/Services/ContextService.js +9 -5
- package/dist/Services/DescriptionService.d.ts +2 -1
- package/dist/Services/DescriptionService.js +8 -2
- package/dist/Services/DetailService.d.ts +7 -0
- package/dist/Services/DetailService.js +18 -0
- package/dist/Services/FocusCritiqueService.d.ts +23 -0
- package/dist/Services/FocusCritiqueService.js +166 -0
- package/dist/Services/FramingService.d.ts +31 -0
- package/dist/Services/FramingService.js +108 -0
- package/dist/Services/GazeService.js +3 -1
- package/dist/Services/GroundDetailService.d.ts +50 -0
- package/dist/Services/GroundDetailService.js +115 -0
- package/dist/Services/KitService.d.ts +11 -2
- package/dist/Services/KitService.js +45 -10
- package/dist/Services/LightingService.d.ts +15 -0
- package/dist/Services/LightingService.js +124 -6
- package/dist/Services/PaletteService.d.ts +108 -0
- package/dist/Services/PaletteService.js +282 -0
- package/dist/Services/PieceService.d.ts +4 -0
- package/dist/Services/PieceService.js +28 -7
- package/dist/Services/PlacementService.js +7 -1
- package/dist/Services/PlaneBackgroundService.d.ts +4 -0
- package/dist/Services/PlaneBackgroundService.js +42 -3
- package/dist/Services/PlaneProjectionService.d.ts +4 -0
- package/dist/Services/PlaneProjectionService.js +33 -4
- package/dist/Services/RasterService.js +20 -3
- package/dist/Services/RidgeService.d.ts +31 -0
- package/dist/Services/RidgeService.js +85 -0
- package/dist/Services/ScaleCritiqueService.d.ts +11 -0
- package/dist/Services/ScaleCritiqueService.js +57 -0
- package/dist/Services/SkyDetailService.d.ts +39 -0
- package/dist/Services/SkyDetailService.js +95 -0
- package/dist/Services/{ContactCritiqueService.d.ts → TangentCritiqueService.d.ts} +10 -6
- package/dist/Services/TangentCritiqueService.js +136 -0
- package/dist/Services/TextService.d.ts +43 -3
- package/dist/Services/TextService.js +288 -32
- package/dist/Services/ThumbnailCritiqueService.d.ts +30 -0
- package/dist/Services/ThumbnailCritiqueService.js +130 -0
- package/dist/Utils/ColorUtil.d.ts +14 -0
- package/dist/Utils/ColorUtil.js +68 -5
- package/dist/Utils/DetailUtil.d.ts +7 -0
- package/dist/Utils/DetailUtil.js +15 -0
- package/dist/Utils/FontMetricsUtil.d.ts +7 -0
- package/dist/Utils/FontMetricsUtil.js +694 -0
- package/dist/Utils/OklabUtil.d.ts +18 -0
- package/dist/Utils/OklabUtil.js +89 -0
- package/dist/Utils/PersonArmUtil.d.ts +13 -1
- package/dist/Utils/PersonArmUtil.js +53 -5
- package/dist/Utils/PersonHeadUtil.js +3 -1
- package/dist/Utils/PersonOutfitUtil.d.ts +16 -0
- package/dist/Utils/PersonOutfitUtil.js +89 -13
- package/dist/Utils/PersonUtil.d.ts +2 -0
- package/dist/Utils/PersonUtil.js +14 -4
- package/dist/Utils/SanitizeUtil.js +1 -0
- package/dist/Utils/ShapeUtil.d.ts +9 -0
- package/dist/Utils/ShapeUtil.js +26 -0
- package/dist/Utils/TypesettingUtil.d.ts +17 -0
- package/dist/Utils/TypesettingUtil.js +92 -0
- package/dist/cli.js +32 -3
- package/dist/render.d.ts +3 -1
- package/dist/render.js +5 -0
- package/fonts/DMSerifDisplay-Regular.ttf +0 -0
- package/fonts/OFL.txt +2 -0
- package/fonts/PatrickHand-Regular.ttf +0 -0
- package/package.json +1 -1
- package/skills/draw-scene/SKILL.md +2 -1
- package/skills/draw-scene/recipes/carousel.yaml +1 -1
- package/skills/draw-scene/recipes/conference-keynote.yaml +43 -0
- package/skills/draw-scene/recipes/product-feature.yaml +41 -0
- package/skills/draw-scene/recipes/product-illustration.yaml +2 -1
- package/skills/draw-scene/references/catalog.md +19 -1
- package/skills/draw-scene/references/dsl.md +24 -6
- package/dist/Services/ContactCritiqueService.js +0 -101
|
@@ -1,8 +1,10 @@
|
|
|
1
|
+
import { KitService } from "./KitService.js";
|
|
1
2
|
import { ColorUtil } from "../Utils/ColorUtil.js";
|
|
3
|
+
import { FontMetricsUtil } from "../Utils/FontMetricsUtil.js";
|
|
2
4
|
import { IdUtil } from "../Utils/IdUtil.js";
|
|
5
|
+
import { TypesettingUtil } from "../Utils/TypesettingUtil.js";
|
|
3
6
|
const HORIZONTAL_NAMES = ["left", "center", "right"];
|
|
4
7
|
class TextService {
|
|
5
|
-
static FONT = "Poppins";
|
|
6
8
|
static TITLE_SIZE = 0.075;
|
|
7
9
|
static SUBTITLE_SIZE = 0.036;
|
|
8
10
|
static LABEL_SIZE = 0.028;
|
|
@@ -33,6 +35,11 @@ class TextService {
|
|
|
33
35
|
leftOf: (anchorX, textWidth) => anchorX - textWidth
|
|
34
36
|
}
|
|
35
37
|
};
|
|
38
|
+
static ANCHOR_TO_LEFT_OF = {
|
|
39
|
+
start: (anchorX) => anchorX,
|
|
40
|
+
middle: (anchorX, textWidth) => anchorX - textWidth * TextService.HALF,
|
|
41
|
+
end: (anchorX, textWidth) => anchorX - textWidth
|
|
42
|
+
};
|
|
36
43
|
static DEFAULT_SIZE = 0.04;
|
|
37
44
|
static DEFAULT_WEIGHT = 500;
|
|
38
45
|
static MARGIN = 0.06;
|
|
@@ -40,8 +47,16 @@ class TextService {
|
|
|
40
47
|
static BASELINE_ASCENT = 0.9;
|
|
41
48
|
static SUBTITLE_GAP = 0.8;
|
|
42
49
|
static LINE_HEIGHT = 1.2;
|
|
43
|
-
static
|
|
44
|
-
|
|
50
|
+
static FONT_NAME_TO_FAMILY = {
|
|
51
|
+
sans: "Poppins",
|
|
52
|
+
serif: "DM Serif Display",
|
|
53
|
+
hand: "Patrick Hand"
|
|
54
|
+
};
|
|
55
|
+
static FONT_TO_CHAR_WIDTH = {
|
|
56
|
+
sans: { regular: 0.565, bold: 0.6 },
|
|
57
|
+
serif: { regular: 0.49, bold: 0.53 },
|
|
58
|
+
hand: { regular: 0.42, bold: 0.45 }
|
|
59
|
+
};
|
|
45
60
|
static MIN_CONTRAST = 4.5;
|
|
46
61
|
static SELF_COVER = 0.06;
|
|
47
62
|
static OBJECT_COVER = 0.12;
|
|
@@ -53,20 +68,48 @@ class TextService {
|
|
|
53
68
|
static BOX_TOP_PAD = 0.3;
|
|
54
69
|
static BOX_BOTTOM_EXTRA = 0.45;
|
|
55
70
|
static BOX_BOTTOM_PAD = 0.6;
|
|
71
|
+
static TRACKING_THRESHOLD_PX = 36;
|
|
72
|
+
static DEFAULT_TIGHT_TRACKING = -0.015;
|
|
73
|
+
static TRACKING_DECIMALS = 3;
|
|
74
|
+
static DEFAULT_STICKER_ROTATION = -1.5;
|
|
75
|
+
static STICKER_STROKE_FRACTION = 0.12;
|
|
76
|
+
static STICKER_MIN_STROKE = 3;
|
|
77
|
+
static STICKER_SHADOW_DY = 0.1;
|
|
78
|
+
static STICKER_SHADOW_BLUR = 0.12;
|
|
79
|
+
static STICKER_SHADOW_OPACITY = 0.25;
|
|
80
|
+
static PILL_HORIZONTAL_PAD = 0.6;
|
|
81
|
+
static PILL_VERTICAL_PAD = 0.35;
|
|
82
|
+
static PILL_SHADOW_DY = 0.08;
|
|
83
|
+
static PILL_SHADOW_BLUR = 0.12;
|
|
84
|
+
static PILL_SHADOW_OPACITY = 0.22;
|
|
85
|
+
static HIGHLIGHT_TOP = 0.78;
|
|
86
|
+
static HIGHLIGHT_BOTTOM = 0.15;
|
|
87
|
+
static HIGHLIGHT_PAD = 0.2;
|
|
88
|
+
static HIGHLIGHT_SKEW = 0.08;
|
|
89
|
+
static HIGHLIGHT_OPACITY = 0.38;
|
|
90
|
+
static UNDERLINE_Y_OFFSET = 0.16;
|
|
91
|
+
static HALF_CIRCLE_DEG = 180;
|
|
92
|
+
static DEG_TO_RAD = Math.PI / TextService.HALF_CIRCLE_DEG;
|
|
93
|
+
static HALF = 0.5;
|
|
94
|
+
static STYLE_IGNORES_CONTRAST = /* @__PURE__ */ new Set(["highlight", "underline"]);
|
|
95
|
+
static BOX_STYLE_TO_RECT = {
|
|
96
|
+
rect: (layout) => TextService.rectForDefault(layout),
|
|
97
|
+
pill: (layout) => TextService.rectForPill(layout),
|
|
98
|
+
sticker: (layout) => TextService.rectForSticker(layout),
|
|
99
|
+
highlight: (layout) => TextService.rectForHighlight(layout),
|
|
100
|
+
underline: (layout) => TextService.rectForUnderline(layout)
|
|
101
|
+
};
|
|
56
102
|
static render(texts, context, list, backdrop) {
|
|
57
103
|
const rects = [];
|
|
58
104
|
let svg = "";
|
|
59
|
-
for (
|
|
60
|
-
const
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
hasBox: Boolean(text.box)
|
|
68
|
-
});
|
|
69
|
-
svg += TextService.boxOf(text, layout, context, backdrop) + TextService.element(layout);
|
|
105
|
+
for (let index = 0; index < texts.length; index++) {
|
|
106
|
+
const text = texts[index];
|
|
107
|
+
if (!text) {
|
|
108
|
+
continue;
|
|
109
|
+
}
|
|
110
|
+
const layout = TextService.layoutOf(text, texts, context, index);
|
|
111
|
+
rects.push(TextService.rectOf(layout));
|
|
112
|
+
svg += TextService.boxOf(layout, context, backdrop) + TextService.element(layout);
|
|
70
113
|
TextService.warnCovered(layout, list, context);
|
|
71
114
|
}
|
|
72
115
|
return { svg, rects };
|
|
@@ -78,7 +121,7 @@ class TextService {
|
|
|
78
121
|
static isRole(text, role) {
|
|
79
122
|
return (text.role ?? "title") === role;
|
|
80
123
|
}
|
|
81
|
-
static layoutOf(text, texts, context) {
|
|
124
|
+
static layoutOf(text, texts, context, index) {
|
|
82
125
|
const { width, height, theme } = context;
|
|
83
126
|
const role = text.role ?? "title";
|
|
84
127
|
const roleSize = IdUtil.lookup(TextService.ROLE_TO_SIZE, role) ?? TextService.DEFAULT_SIZE;
|
|
@@ -89,13 +132,22 @@ class TextService {
|
|
|
89
132
|
const margin = width * TextService.MARGIN;
|
|
90
133
|
const defaultX = alignment.startX(width, margin);
|
|
91
134
|
const anchorX = text.x != null ? text.x * width : defaultX;
|
|
92
|
-
const
|
|
135
|
+
const kit = context.canvas.kit ? IdUtil.lookup(KitService.KIT_ID_TO_KIT, context.canvas.kit) : void 0;
|
|
136
|
+
const fontName = text.font ?? context.canvas.font ?? kit?.font ?? "sans";
|
|
137
|
+
const fontFamily = TextService.FONT_NAME_TO_FAMILY[fontName];
|
|
138
|
+
const tracking = TextService.resolveTracking(text.tracking, fontSize);
|
|
139
|
+
const lines = TypesettingUtil.splitLines(String(text.content), {
|
|
140
|
+
wrap: text.wrap,
|
|
141
|
+
hasBalance: text.balance ?? (role === "title" && text.wrap != null)
|
|
142
|
+
});
|
|
93
143
|
const lineHeight = fontSize * TextService.LINE_HEIGHT;
|
|
94
144
|
const weight = text.weight ?? IdUtil.lookup(TextService.ROLE_TO_WEIGHT, role) ?? TextService.DEFAULT_WEIGHT;
|
|
95
145
|
const isBold = weight >= TextService.BOLD_WEIGHT || IdUtil.lookup(TextService.ROLE_TO_IS_BOLD, role) === true;
|
|
96
|
-
const
|
|
97
|
-
const
|
|
146
|
+
const charWidths = TextService.FONT_TO_CHAR_WIDTH[fontName];
|
|
147
|
+
const charWidth = isBold ? charWidths.bold : charWidths.regular;
|
|
148
|
+
const estimatedWidth = Math.max(...lines.map((line) => line.length)) * fontSize * (charWidth + tracking);
|
|
98
149
|
const metrics = { isBottom: vertical === "bottom", margin, fontSize, lineHeight, lineCount: lines.length };
|
|
150
|
+
const boxFill = text.box ? TextService.themeColor(context, text.box) : void 0;
|
|
99
151
|
return {
|
|
100
152
|
role,
|
|
101
153
|
fontSize,
|
|
@@ -108,9 +160,28 @@ class TextService {
|
|
|
108
160
|
left: alignment.leftOf(anchorX, estimatedWidth),
|
|
109
161
|
y: TextService.baseline(text, texts, metrics, context),
|
|
110
162
|
color: text.color ? TextService.themeColor(context, text.color) : theme.text,
|
|
111
|
-
blockHeight: fontSize + (lines.length - 1) * lineHeight
|
|
163
|
+
blockHeight: fontSize + (lines.length - 1) * lineHeight,
|
|
164
|
+
fontFamily,
|
|
165
|
+
fontName,
|
|
166
|
+
isBold,
|
|
167
|
+
tracking,
|
|
168
|
+
charWidth,
|
|
169
|
+
boxStyle: text.box_style ?? (text.box ? "rect" : void 0),
|
|
170
|
+
rotation: text.rotation,
|
|
171
|
+
emphasis: text.emphasis,
|
|
172
|
+
hasBox: Boolean(text.box ?? text.box_style),
|
|
173
|
+
boxFill,
|
|
174
|
+
pad: text.pad,
|
|
175
|
+
radius: text.radius,
|
|
176
|
+
index
|
|
112
177
|
};
|
|
113
178
|
}
|
|
179
|
+
static resolveTracking(tracking, fontSize = 0) {
|
|
180
|
+
if (tracking != null) {
|
|
181
|
+
return tracking;
|
|
182
|
+
}
|
|
183
|
+
return fontSize >= TextService.TRACKING_THRESHOLD_PX ? TextService.DEFAULT_TIGHT_TRACKING : 0;
|
|
184
|
+
}
|
|
114
185
|
static baseline(text, texts, metrics, context) {
|
|
115
186
|
const { isBottom, margin, fontSize, lineHeight, lineCount } = metrics;
|
|
116
187
|
if (text.y != null) {
|
|
@@ -122,23 +193,203 @@ class TextService {
|
|
|
122
193
|
const shift = context.height * TextService.TITLE_SIZE * TextService.BASELINE_ASCENT + fontSize * TextService.SUBTITLE_GAP;
|
|
123
194
|
return hasTitleAbove ? base + shift : base;
|
|
124
195
|
}
|
|
125
|
-
static
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
196
|
+
static rectOf(layout) {
|
|
197
|
+
if (layout.boxStyle) {
|
|
198
|
+
return TextService.BOX_STYLE_TO_RECT[layout.boxStyle](layout);
|
|
199
|
+
}
|
|
200
|
+
return TextService.rectForPlain(layout);
|
|
201
|
+
}
|
|
202
|
+
static rectForPlain(layout) {
|
|
203
|
+
const { left, y, fontSize, estimatedWidth, blockHeight } = layout;
|
|
204
|
+
return {
|
|
205
|
+
x0: left,
|
|
206
|
+
y0: y - fontSize,
|
|
207
|
+
x1: left + estimatedWidth,
|
|
208
|
+
y1: y + blockHeight - fontSize * TextService.RECT_DESCENT,
|
|
209
|
+
hasBox: false
|
|
210
|
+
};
|
|
211
|
+
}
|
|
212
|
+
static rectForDefault(layout) {
|
|
213
|
+
const { left, y, fontSize, estimatedWidth, blockHeight, pad } = layout;
|
|
214
|
+
const padding = (pad ?? TextService.DEFAULT_PAD) * fontSize;
|
|
215
|
+
return {
|
|
216
|
+
x0: left - padding,
|
|
217
|
+
y0: y - fontSize * TextService.BOX_ASCENT - padding * TextService.BOX_TOP_PAD,
|
|
218
|
+
x1: left + estimatedWidth + padding,
|
|
219
|
+
y1: y + blockHeight + fontSize * TextService.BOX_BOTTOM_EXTRA + padding * TextService.BOX_BOTTOM_PAD,
|
|
220
|
+
hasBox: true
|
|
221
|
+
};
|
|
222
|
+
}
|
|
223
|
+
static rectForPill(layout) {
|
|
224
|
+
const { left, y, fontSize, estimatedWidth, blockHeight, pad } = layout;
|
|
225
|
+
const padding = (pad ?? TextService.PILL_HORIZONTAL_PAD) * fontSize;
|
|
226
|
+
return {
|
|
227
|
+
x0: left - padding,
|
|
228
|
+
y0: y - fontSize * TextService.BOX_ASCENT,
|
|
229
|
+
x1: left + estimatedWidth + padding,
|
|
230
|
+
y1: y + blockHeight + fontSize * TextService.BOX_BOTTOM_EXTRA,
|
|
231
|
+
hasBox: true
|
|
232
|
+
};
|
|
233
|
+
}
|
|
234
|
+
static rectForSticker(layout) {
|
|
235
|
+
const { left, y, fontSize, estimatedWidth, blockHeight, pad, rotation } = layout;
|
|
236
|
+
const padding = (pad ?? TextService.DEFAULT_PAD) * fontSize;
|
|
237
|
+
const angle = (rotation ?? TextService.DEFAULT_STICKER_ROTATION) * TextService.DEG_TO_RAD;
|
|
238
|
+
const rawWidth = estimatedWidth + padding * 2;
|
|
239
|
+
const rawHeight = blockHeight + fontSize * (TextService.BOX_ASCENT + TextService.BOX_BOTTOM_EXTRA);
|
|
240
|
+
const rotWidth = rawWidth * Math.cos(angle) + rawHeight * Math.abs(Math.sin(angle));
|
|
241
|
+
const rotHeight = rawWidth * Math.abs(Math.sin(angle)) + rawHeight * Math.cos(angle);
|
|
242
|
+
const centerX = left + estimatedWidth * TextService.HALF;
|
|
243
|
+
const centerY = y + (blockHeight - fontSize) * TextService.HALF;
|
|
244
|
+
return {
|
|
245
|
+
x0: centerX - rotWidth * TextService.HALF,
|
|
246
|
+
y0: centerY - rotHeight * TextService.HALF,
|
|
247
|
+
x1: centerX + rotWidth * TextService.HALF,
|
|
248
|
+
y1: centerY + rotHeight * TextService.HALF,
|
|
249
|
+
hasBox: true
|
|
250
|
+
};
|
|
251
|
+
}
|
|
252
|
+
static rectForHighlight(layout) {
|
|
253
|
+
const { left, y, fontSize, estimatedWidth, blockHeight } = layout;
|
|
254
|
+
const padding = fontSize * TextService.HIGHLIGHT_PAD;
|
|
255
|
+
return {
|
|
256
|
+
x0: left - padding,
|
|
257
|
+
y0: y - fontSize * TextService.HIGHLIGHT_TOP,
|
|
258
|
+
x1: left + estimatedWidth + padding,
|
|
259
|
+
y1: y + blockHeight + fontSize * TextService.HIGHLIGHT_BOTTOM,
|
|
260
|
+
hasBox: true
|
|
261
|
+
};
|
|
262
|
+
}
|
|
263
|
+
static rectForUnderline(layout) {
|
|
264
|
+
const { left, y, fontSize, estimatedWidth, blockHeight } = layout;
|
|
265
|
+
return {
|
|
266
|
+
x0: left,
|
|
267
|
+
y0: y - fontSize,
|
|
268
|
+
x1: left + estimatedWidth,
|
|
269
|
+
y1: y + blockHeight + fontSize * TextService.UNDERLINE_Y_OFFSET,
|
|
270
|
+
hasBox: true
|
|
271
|
+
};
|
|
272
|
+
}
|
|
273
|
+
static boxOf(layout, context, backdrop) {
|
|
274
|
+
const { color, lines, boxStyle, boxFill } = layout;
|
|
275
|
+
const effectiveFill = boxFill ?? backdrop;
|
|
276
|
+
const ratio = ColorUtil.contrast(color, effectiveFill);
|
|
277
|
+
const isContrastIgnored = boxStyle ? TextService.STYLE_IGNORES_CONTRAST.has(boxStyle) : false;
|
|
278
|
+
if (ratio < TextService.MIN_CONTRAST && !isContrastIgnored) {
|
|
130
279
|
context.warnings.push(
|
|
131
280
|
`text "${lines[0]}" has contrast ${ratio.toFixed(1)}:1 (min. 4.5); change 'color' or use 'box'`
|
|
132
281
|
);
|
|
133
282
|
}
|
|
134
|
-
if (!
|
|
283
|
+
if (!boxStyle) {
|
|
135
284
|
return "";
|
|
136
285
|
}
|
|
137
|
-
const
|
|
286
|
+
const boxStyleToRenderer = {
|
|
287
|
+
rect: () => TextService.renderRectBox(layout, effectiveFill),
|
|
288
|
+
pill: () => TextService.renderPillBox(layout, context),
|
|
289
|
+
sticker: () => TextService.renderStickerBox(layout, context),
|
|
290
|
+
highlight: () => TextService.renderHighlightBox(layout, context),
|
|
291
|
+
underline: () => TextService.renderUnderline(layout, context)
|
|
292
|
+
};
|
|
293
|
+
return boxStyleToRenderer[boxStyle]();
|
|
294
|
+
}
|
|
295
|
+
static renderRectBox(layout, fill) {
|
|
296
|
+
const { fontSize, left, estimatedWidth, blockHeight, y, pad, radius } = layout;
|
|
297
|
+
const padding = (pad ?? TextService.DEFAULT_PAD) * fontSize;
|
|
138
298
|
const boxY = y - fontSize * TextService.BOX_ASCENT - padding * TextService.BOX_TOP_PAD;
|
|
139
299
|
const boxHeight = blockHeight + fontSize * TextService.BOX_BOTTOM_EXTRA + padding * TextService.BOX_BOTTOM_PAD;
|
|
140
|
-
const
|
|
141
|
-
return `<rect x="${(left - padding).toFixed(1)}" y="${boxY.toFixed(1)}" width="${(estimatedWidth + padding * 2).toFixed(1)}" height="${boxHeight.toFixed(1)}" rx="${
|
|
300
|
+
const cornerRadius = (radius ?? TextService.DEFAULT_RADIUS) * fontSize;
|
|
301
|
+
return `<rect x="${(left - padding).toFixed(1)}" y="${boxY.toFixed(1)}" width="${(estimatedWidth + padding * 2).toFixed(1)}" height="${boxHeight.toFixed(1)}" rx="${cornerRadius.toFixed(1)}" fill="${fill}"/>`;
|
|
302
|
+
}
|
|
303
|
+
static renderPillBox(layout, context) {
|
|
304
|
+
const { fontSize, left, estimatedWidth, blockHeight, y, boxFill, pad, index } = layout;
|
|
305
|
+
const fill = boxFill ?? context.theme.accent;
|
|
306
|
+
const padding = (pad ?? TextService.PILL_HORIZONTAL_PAD) * fontSize;
|
|
307
|
+
const boxY = y - fontSize * TextService.BOX_ASCENT;
|
|
308
|
+
const boxHeight = blockHeight + fontSize * (TextService.BOX_ASCENT + TextService.PILL_VERTICAL_PAD);
|
|
309
|
+
const boxWidth = estimatedWidth + padding * 2;
|
|
310
|
+
const radius = boxHeight * TextService.HALF;
|
|
311
|
+
const shadowDy = (fontSize * TextService.PILL_SHADOW_DY).toFixed(1);
|
|
312
|
+
const shadowBlur = (fontSize * TextService.PILL_SHADOW_BLUR).toFixed(1);
|
|
313
|
+
const filterId = `pill_sh_${index}`;
|
|
314
|
+
return `<defs><filter id="${filterId}" x="-20%" y="-20%" width="140%" height="150%"><feDropShadow dx="0" dy="${shadowDy}" stdDeviation="${shadowBlur}" flood-color="#000000" flood-opacity="${TextService.PILL_SHADOW_OPACITY}"/></filter></defs><rect x="${(left - padding).toFixed(1)}" y="${boxY.toFixed(1)}" width="${boxWidth.toFixed(1)}" height="${boxHeight.toFixed(1)}" rx="${radius.toFixed(1)}" fill="${fill}" filter="url(#${filterId})"/>`;
|
|
315
|
+
}
|
|
316
|
+
static renderStickerBox(layout, context) {
|
|
317
|
+
const { fontSize, left, estimatedWidth, blockHeight, y, boxFill, pad, radius, rotation, index } = layout;
|
|
318
|
+
const fill = boxFill ?? context.theme.paper;
|
|
319
|
+
const padding = (pad ?? TextService.DEFAULT_PAD) * fontSize;
|
|
320
|
+
const boxY = y - fontSize * TextService.BOX_ASCENT;
|
|
321
|
+
const boxHeight = blockHeight + fontSize * (TextService.BOX_ASCENT + TextService.BOX_BOTTOM_EXTRA);
|
|
322
|
+
const boxWidth = estimatedWidth + padding * 2;
|
|
323
|
+
const cornerRadius = (radius ?? TextService.DEFAULT_RADIUS) * fontSize;
|
|
324
|
+
const strokeWidth = Math.max(TextService.STICKER_MIN_STROKE, fontSize * TextService.STICKER_STROKE_FRACTION);
|
|
325
|
+
const shadowDy = (fontSize * TextService.STICKER_SHADOW_DY).toFixed(1);
|
|
326
|
+
const shadowBlur = (fontSize * TextService.STICKER_SHADOW_BLUR).toFixed(1);
|
|
327
|
+
const filterId = `stk_sh_${index}`;
|
|
328
|
+
const angle = rotation ?? TextService.DEFAULT_STICKER_ROTATION;
|
|
329
|
+
const centerX = left + estimatedWidth * TextService.HALF;
|
|
330
|
+
const centerY = boxY + boxHeight * TextService.HALF;
|
|
331
|
+
return `<defs><filter id="${filterId}" x="-25%" y="-25%" width="150%" height="160%"><feDropShadow dx="1" dy="${shadowDy}" stdDeviation="${shadowBlur}" flood-color="#000000" flood-opacity="${TextService.STICKER_SHADOW_OPACITY}"/></filter></defs><g transform="rotate(${angle.toFixed(1)}, ${centerX.toFixed(1)}, ${centerY.toFixed(1)})"><rect x="${(left - padding).toFixed(1)}" y="${boxY.toFixed(1)}" width="${boxWidth.toFixed(1)}" height="${boxHeight.toFixed(1)}" rx="${cornerRadius.toFixed(1)}" fill="${fill}" stroke="#FFFFFF" stroke-width="${strokeWidth.toFixed(1)}" stroke-linejoin="round" filter="url(#${filterId})"/>`;
|
|
332
|
+
}
|
|
333
|
+
static renderHighlightBox(layout, context) {
|
|
334
|
+
const { fontSize, x, y, lines, lineHeight, anchor, fontName, isBold, tracking, boxFill } = layout;
|
|
335
|
+
const fill = boxFill ?? context.theme.accent;
|
|
336
|
+
const pad = fontSize * TextService.HIGHLIGHT_PAD;
|
|
337
|
+
const skew = fontSize * TextService.HIGHLIGHT_SKEW;
|
|
338
|
+
let svg = "";
|
|
339
|
+
for (let lineIndex = 0; lineIndex < lines.length; lineIndex++) {
|
|
340
|
+
const line = lines[lineIndex];
|
|
341
|
+
if (!line) {
|
|
342
|
+
continue;
|
|
343
|
+
}
|
|
344
|
+
const lineY = y + lineIndex * lineHeight;
|
|
345
|
+
const lineWidth = fontSize * (FontMetricsUtil.lineEm(line, fontName, isBold) + tracking * line.length);
|
|
346
|
+
const lineLeft = TextService.ANCHOR_TO_LEFT_OF[anchor](x, lineWidth);
|
|
347
|
+
const top = lineY - fontSize * TextService.HIGHLIGHT_TOP;
|
|
348
|
+
const bottom = lineY + fontSize * TextService.HIGHLIGHT_BOTTOM;
|
|
349
|
+
const points = [
|
|
350
|
+
`${(lineLeft - pad + skew).toFixed(1)},${top.toFixed(1)}`,
|
|
351
|
+
`${(lineLeft + lineWidth + pad + skew).toFixed(1)},${top.toFixed(1)}`,
|
|
352
|
+
`${(lineLeft + lineWidth + pad - skew).toFixed(1)},${bottom.toFixed(1)}`,
|
|
353
|
+
`${(lineLeft - pad - skew).toFixed(1)},${bottom.toFixed(1)}`
|
|
354
|
+
].join(" ");
|
|
355
|
+
svg += `<polygon points="${points}" fill="${fill}" fill-opacity="${TextService.HIGHLIGHT_OPACITY}"/>`;
|
|
356
|
+
}
|
|
357
|
+
return svg;
|
|
358
|
+
}
|
|
359
|
+
static renderUnderline(layout, context) {
|
|
360
|
+
const { fontSize, x, y, lines, lineHeight, anchor, weight, fontFamily, tracking, boxFill, emphasis } = layout;
|
|
361
|
+
const stroke = boxFill ?? context.theme.accent;
|
|
362
|
+
const spans = lines.map((line, lineIndex) => {
|
|
363
|
+
const dy = lineIndex === 0 ? "" : ` dy="${lineHeight.toFixed(1)}"`;
|
|
364
|
+
const pieces = TextService.underlinedPieces(line, emphasis, stroke);
|
|
365
|
+
return `<tspan x="${x.toFixed(1)}"${dy}>${pieces}</tspan>`;
|
|
366
|
+
});
|
|
367
|
+
const trackingAttr = tracking !== 0 ? ` letter-spacing="${tracking.toFixed(TextService.TRACKING_DECIMALS)}em"` : "";
|
|
368
|
+
return `<text x="${x.toFixed(1)}" y="${y.toFixed(1)}" font-family="${fontFamily}" font-size="${fontSize.toFixed(1)}" font-weight="${weight}" fill="${stroke}" fill-opacity="0" text-anchor="${anchor}"${trackingAttr}>${spans.join("")}</text>`;
|
|
369
|
+
}
|
|
370
|
+
static underlinedPieces(line, emphasis, stroke) {
|
|
371
|
+
const ranges = TypesettingUtil.emphasisRanges(line, emphasis);
|
|
372
|
+
const merged = [];
|
|
373
|
+
for (const range of ranges) {
|
|
374
|
+
const end = range.start + range.length;
|
|
375
|
+
const last = merged.at(-1);
|
|
376
|
+
if (last && range.start <= last.end) {
|
|
377
|
+
last.end = Math.max(last.end, end);
|
|
378
|
+
} else {
|
|
379
|
+
merged.push({ start: range.start, end });
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
let cursor = 0;
|
|
383
|
+
let html = "";
|
|
384
|
+
for (const { start, end } of merged) {
|
|
385
|
+
html += TextService.escapeText(line.slice(cursor, start));
|
|
386
|
+
html += `<tspan fill="${stroke}" fill-opacity="1" text-decoration="underline">${TextService.escapeText(line.slice(start, end))}</tspan>`;
|
|
387
|
+
cursor = end;
|
|
388
|
+
}
|
|
389
|
+
return html + TextService.escapeText(line.slice(cursor));
|
|
390
|
+
}
|
|
391
|
+
static escapeText(value) {
|
|
392
|
+
return value.replace(/&/g, "&").replace(/</g, "<");
|
|
142
393
|
}
|
|
143
394
|
static warnCovered(layout, list, context) {
|
|
144
395
|
const { left, estimatedWidth, y, blockHeight, fontSize, lines } = layout;
|
|
@@ -164,13 +415,18 @@ class TextService {
|
|
|
164
415
|
}
|
|
165
416
|
}
|
|
166
417
|
static element(layout) {
|
|
167
|
-
const { x, y, fontSize, weight, color, anchor, lines, lineHeight } = layout;
|
|
418
|
+
const { x, y, fontSize, weight, color, anchor, lines, lineHeight, fontFamily, tracking, boxStyle } = layout;
|
|
168
419
|
const escape = (value) => value.replace(/&/g, "&").replace(/</g, "<");
|
|
169
|
-
const spans = lines.map((line,
|
|
170
|
-
const dy =
|
|
420
|
+
const spans = lines.map((line, lineIndex) => {
|
|
421
|
+
const dy = lineIndex === 0 ? "" : ` dy="${lineHeight.toFixed(1)}"`;
|
|
171
422
|
return `<tspan x="${x.toFixed(1)}"${dy}>${escape(line)}</tspan>`;
|
|
172
423
|
});
|
|
173
|
-
|
|
424
|
+
const trackingAttr = tracking !== 0 ? ` letter-spacing="${tracking.toFixed(TextService.TRACKING_DECIMALS)}em"` : "";
|
|
425
|
+
const textTag = `<text x="${x.toFixed(1)}" y="${y.toFixed(1)}" font-family="${fontFamily}" font-size="${fontSize.toFixed(1)}" font-weight="${weight}" fill="${color}" text-anchor="${anchor}"${trackingAttr}>${spans.join("")}</text>`;
|
|
426
|
+
if (boxStyle === "sticker") {
|
|
427
|
+
return `${textTag}</g>`;
|
|
428
|
+
}
|
|
429
|
+
return textTag;
|
|
174
430
|
}
|
|
175
431
|
}
|
|
176
432
|
export {
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { CritiqueTitle } from "../Protocols/CompilationProtocol.js";
|
|
2
|
+
export interface ThumbnailCritiqueInput {
|
|
3
|
+
thumbnailPixels: Uint8Array;
|
|
4
|
+
thumbnailWidth: number;
|
|
5
|
+
thumbnailHeight: number;
|
|
6
|
+
sceneWidth: number;
|
|
7
|
+
focusObject?: {
|
|
8
|
+
id: string;
|
|
9
|
+
pixels: Uint8Array;
|
|
10
|
+
};
|
|
11
|
+
title?: CritiqueTitle;
|
|
12
|
+
}
|
|
13
|
+
export declare class ThumbnailCritiqueService {
|
|
14
|
+
private static readonly OPAQUE_ALPHA;
|
|
15
|
+
private static readonly DILATION_PX;
|
|
16
|
+
private static readonly MIN_FOCUS_PIXELS;
|
|
17
|
+
private static readonly MIN_CONTRAST;
|
|
18
|
+
private static readonly CAP_HEIGHT_RATIO;
|
|
19
|
+
private static readonly MIN_CAP_HEIGHT_PX;
|
|
20
|
+
private static readonly CHANNELS;
|
|
21
|
+
private static readonly ALPHA_OFFSET;
|
|
22
|
+
private static readonly RED_OFFSET;
|
|
23
|
+
private static readonly GREEN_OFFSET;
|
|
24
|
+
private static readonly BLUE_OFFSET;
|
|
25
|
+
static critique(input: ThumbnailCritiqueInput): string[];
|
|
26
|
+
private static checkTitle;
|
|
27
|
+
private static checkFocus;
|
|
28
|
+
private static surroundContrast;
|
|
29
|
+
private static dilate;
|
|
30
|
+
}
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
import { OklabUtil } from "../Utils/OklabUtil.js";
|
|
2
|
+
import { PixelUtil } from "../Utils/PixelUtil.js";
|
|
3
|
+
class ThumbnailCritiqueService {
|
|
4
|
+
// Why: alpha above 128 marks the solid perceived shape of an SVG element.
|
|
5
|
+
static OPAQUE_ALPHA = 128;
|
|
6
|
+
// Why: dilating by 2 px isolates the immediate background boundary around the thumbnail silhouette.
|
|
7
|
+
static DILATION_PX = 2;
|
|
8
|
+
// Why: at 96 px wide, a focal subject must cover at least 20 pixels to be clearly recognizable.
|
|
9
|
+
static MIN_FOCUS_PIXELS = 20;
|
|
10
|
+
// Why: local lightness contrast below 0.035 causes the focal silhouette to blend into its background at thumbnail size.
|
|
11
|
+
static MIN_CONTRAST = 0.035;
|
|
12
|
+
// Why: Poppins font capital letter height is approximately 70% of the em-box font size.
|
|
13
|
+
static CAP_HEIGHT_RATIO = 0.7;
|
|
14
|
+
// Why: text with cap height below ~4 px cannot be resolved on mobile feeds or grid thumbnails.
|
|
15
|
+
static MIN_CAP_HEIGHT_PX = 3.8;
|
|
16
|
+
// Why: RGBA channels and offsets.
|
|
17
|
+
static CHANNELS = 4;
|
|
18
|
+
static ALPHA_OFFSET = 3;
|
|
19
|
+
static RED_OFFSET = 0;
|
|
20
|
+
static GREEN_OFFSET = 1;
|
|
21
|
+
static BLUE_OFFSET = 2;
|
|
22
|
+
static critique(input) {
|
|
23
|
+
return [
|
|
24
|
+
...ThumbnailCritiqueService.checkFocus(input),
|
|
25
|
+
...ThumbnailCritiqueService.checkTitle(input)
|
|
26
|
+
];
|
|
27
|
+
}
|
|
28
|
+
static checkTitle(input) {
|
|
29
|
+
const { title, thumbnailWidth, sceneWidth } = input;
|
|
30
|
+
if (!title) {
|
|
31
|
+
return [];
|
|
32
|
+
}
|
|
33
|
+
const factor = thumbnailWidth / sceneWidth;
|
|
34
|
+
const capHeight = title.fontSize * factor * ThumbnailCritiqueService.CAP_HEIGHT_RATIO;
|
|
35
|
+
if (capHeight < ThumbnailCritiqueService.MIN_CAP_HEIGHT_PX) {
|
|
36
|
+
const minSharePct = 100 * ThumbnailCritiqueService.MIN_CAP_HEIGHT_PX / (ThumbnailCritiqueService.CAP_HEIGHT_RATIO * thumbnailWidth);
|
|
37
|
+
return [
|
|
38
|
+
`title is too small to read at thumbnail size (cap height ${capHeight.toFixed(1)} px, needs ${ThumbnailCritiqueService.MIN_CAP_HEIGHT_PX} px at ${thumbnailWidth} px wide); use a title size of about ${minSharePct.toFixed(1)}% of the scene width or more`
|
|
39
|
+
];
|
|
40
|
+
}
|
|
41
|
+
return [];
|
|
42
|
+
}
|
|
43
|
+
static checkFocus(input) {
|
|
44
|
+
const { focusObject, thumbnailPixels, thumbnailWidth, thumbnailHeight } = input;
|
|
45
|
+
if (!focusObject) {
|
|
46
|
+
return [];
|
|
47
|
+
}
|
|
48
|
+
const pixelCount = thumbnailWidth * thumbnailHeight;
|
|
49
|
+
let focusPixelCount = 0;
|
|
50
|
+
let sumL = 0;
|
|
51
|
+
for (let index = 0; index < pixelCount; index++) {
|
|
52
|
+
const alphaOffset = index * ThumbnailCritiqueService.CHANNELS + ThumbnailCritiqueService.ALPHA_OFFSET;
|
|
53
|
+
const alpha = PixelUtil.read(focusObject.pixels, alphaOffset);
|
|
54
|
+
if (alpha > ThumbnailCritiqueService.OPAQUE_ALPHA) {
|
|
55
|
+
focusPixelCount++;
|
|
56
|
+
const offset = index * ThumbnailCritiqueService.CHANNELS;
|
|
57
|
+
const [lightness] = OklabUtil.fromRgb(
|
|
58
|
+
PixelUtil.read(thumbnailPixels, offset + ThumbnailCritiqueService.RED_OFFSET),
|
|
59
|
+
PixelUtil.read(thumbnailPixels, offset + ThumbnailCritiqueService.GREEN_OFFSET),
|
|
60
|
+
PixelUtil.read(thumbnailPixels, offset + ThumbnailCritiqueService.BLUE_OFFSET)
|
|
61
|
+
);
|
|
62
|
+
sumL += lightness;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
if (focusPixelCount < ThumbnailCritiqueService.MIN_FOCUS_PIXELS) {
|
|
66
|
+
return [`'${focusObject.id}' is lost at thumbnail size (${focusPixelCount} px); increase its contrast or make it larger`];
|
|
67
|
+
}
|
|
68
|
+
const meanFocusL = sumL / focusPixelCount;
|
|
69
|
+
const surroundContrast = ThumbnailCritiqueService.surroundContrast(input, meanFocusL);
|
|
70
|
+
if (surroundContrast < ThumbnailCritiqueService.MIN_CONTRAST) {
|
|
71
|
+
return [`'${focusObject.id}' is lost at thumbnail size; increase its contrast against the background`];
|
|
72
|
+
}
|
|
73
|
+
return [];
|
|
74
|
+
}
|
|
75
|
+
static surroundContrast(input, meanFocusL) {
|
|
76
|
+
const { focusObject, thumbnailPixels, thumbnailWidth, thumbnailHeight } = input;
|
|
77
|
+
if (!focusObject) {
|
|
78
|
+
return 1;
|
|
79
|
+
}
|
|
80
|
+
const pixelCount = thumbnailWidth * thumbnailHeight;
|
|
81
|
+
const dilated = ThumbnailCritiqueService.dilate(focusObject.pixels, thumbnailWidth, thumbnailHeight);
|
|
82
|
+
let sumSurroundL = 0;
|
|
83
|
+
let surroundCount = 0;
|
|
84
|
+
for (let index = 0; index < pixelCount; index++) {
|
|
85
|
+
const isSurround = dilated[index] === 1 && PixelUtil.read(
|
|
86
|
+
focusObject.pixels,
|
|
87
|
+
index * ThumbnailCritiqueService.CHANNELS + ThumbnailCritiqueService.ALPHA_OFFSET
|
|
88
|
+
) <= ThumbnailCritiqueService.OPAQUE_ALPHA;
|
|
89
|
+
if (isSurround) {
|
|
90
|
+
const offset = index * ThumbnailCritiqueService.CHANNELS;
|
|
91
|
+
const [lightness] = OklabUtil.fromRgb(
|
|
92
|
+
PixelUtil.read(thumbnailPixels, offset + ThumbnailCritiqueService.RED_OFFSET),
|
|
93
|
+
PixelUtil.read(thumbnailPixels, offset + ThumbnailCritiqueService.GREEN_OFFSET),
|
|
94
|
+
PixelUtil.read(thumbnailPixels, offset + ThumbnailCritiqueService.BLUE_OFFSET)
|
|
95
|
+
);
|
|
96
|
+
sumSurroundL += lightness;
|
|
97
|
+
surroundCount++;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
if (surroundCount === 0) {
|
|
101
|
+
return 1;
|
|
102
|
+
}
|
|
103
|
+
const meanSurroundL = sumSurroundL / surroundCount;
|
|
104
|
+
return Math.abs(meanFocusL - meanSurroundL);
|
|
105
|
+
}
|
|
106
|
+
static dilate(pixels, width, height) {
|
|
107
|
+
const pixelCount = width * height;
|
|
108
|
+
const dilated = new Uint8Array(pixelCount);
|
|
109
|
+
const reach = ThumbnailCritiqueService.DILATION_PX;
|
|
110
|
+
for (let index = 0; index < pixelCount; index++) {
|
|
111
|
+
const alphaOffset = index * ThumbnailCritiqueService.CHANNELS + ThumbnailCritiqueService.ALPHA_OFFSET;
|
|
112
|
+
const alpha = PixelUtil.read(pixels, alphaOffset);
|
|
113
|
+
if (alpha > ThumbnailCritiqueService.OPAQUE_ALPHA) {
|
|
114
|
+
const centerRow = Math.trunc(index / width);
|
|
115
|
+
const centerCol = index % width;
|
|
116
|
+
const firstRow = Math.max(0, centerRow - reach);
|
|
117
|
+
const lastRow = Math.min(height - 1, centerRow + reach);
|
|
118
|
+
const firstCol = Math.max(0, centerCol - reach);
|
|
119
|
+
const lastCol = Math.min(width - 1, centerCol + reach);
|
|
120
|
+
for (let row = firstRow; row <= lastRow; row++) {
|
|
121
|
+
dilated.fill(1, row * width + firstCol, row * width + lastCol + 1);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
return dilated;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
export {
|
|
129
|
+
ThumbnailCritiqueService
|
|
130
|
+
};
|
|
@@ -11,7 +11,21 @@ export declare class ColorUtil {
|
|
|
11
11
|
private static readonly GREEN_WEIGHT;
|
|
12
12
|
private static readonly BLUE_WEIGHT;
|
|
13
13
|
private static readonly CONTRAST_FLARE;
|
|
14
|
+
private static readonly SHADOW_L_FACTOR;
|
|
15
|
+
private static readonly LIGHT_L_FACTOR;
|
|
16
|
+
private static readonly SHADOW_HUE_TARGET;
|
|
17
|
+
private static readonly LIGHT_HUE_TARGET;
|
|
18
|
+
private static readonly SHADOW_HUE_PULL;
|
|
19
|
+
private static readonly LIGHT_HUE_PULL;
|
|
20
|
+
private static readonly SHADOW_CHROMA_FACTOR;
|
|
21
|
+
private static readonly LIGHT_CHROMA_FACTOR;
|
|
22
|
+
private static readonly CHROMA_SCALE_MAX;
|
|
23
|
+
private static readonly FULL_TURN;
|
|
24
|
+
private static readonly HALF_TURN;
|
|
14
25
|
static sh(hex: string, amount: number): string;
|
|
26
|
+
private static rotateHue;
|
|
27
|
+
private static toHexChannel;
|
|
28
|
+
static mix(first: string, second: string, weight: number): string;
|
|
15
29
|
private static expand;
|
|
16
30
|
private static channelsOf;
|
|
17
31
|
private static linearize;
|
package/dist/Utils/ColorUtil.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { OklabUtil } from "./OklabUtil.js";
|
|
1
2
|
class ColorUtil {
|
|
2
3
|
static HEX_RADIX = 16;
|
|
3
4
|
static CHANNEL_MAX = 255;
|
|
@@ -11,15 +12,77 @@ class ColorUtil {
|
|
|
11
12
|
static GREEN_WEIGHT = 0.7152;
|
|
12
13
|
static BLUE_WEIGHT = 0.0722;
|
|
13
14
|
static CONTRAST_FLARE = 0.05;
|
|
15
|
+
static SHADOW_L_FACTOR = 0.85;
|
|
16
|
+
static LIGHT_L_FACTOR = 0.85;
|
|
17
|
+
static SHADOW_HUE_TARGET = 4.6;
|
|
18
|
+
static LIGHT_HUE_TARGET = 1.4;
|
|
19
|
+
static SHADOW_HUE_PULL = 0.55;
|
|
20
|
+
static LIGHT_HUE_PULL = 0.35;
|
|
21
|
+
static SHADOW_CHROMA_FACTOR = 0.35;
|
|
22
|
+
static LIGHT_CHROMA_FACTOR = 0.6;
|
|
23
|
+
static CHROMA_SCALE_MAX = 0.04;
|
|
24
|
+
static FULL_TURN = Math.PI * 2;
|
|
25
|
+
static HALF_TURN = Math.PI;
|
|
14
26
|
static sh(hex, amount) {
|
|
15
27
|
if (!hex.startsWith("#")) {
|
|
16
28
|
return hex;
|
|
17
29
|
}
|
|
18
|
-
const
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
30
|
+
const full = ColorUtil.expand(hex.slice(1));
|
|
31
|
+
if (!/^[0-9a-f]{6}$/i.test(full)) {
|
|
32
|
+
return hex;
|
|
33
|
+
}
|
|
34
|
+
const [red = 0, green = 0, blue = 0] = ColorUtil.channelsOf(full);
|
|
35
|
+
const lab = OklabUtil.fromRgb(red, green, blue);
|
|
36
|
+
const [lightness, chroma, hue] = OklabUtil.labToLch(lab);
|
|
37
|
+
const strength = Math.min(1, Math.abs(amount));
|
|
38
|
+
const chromaScale = Math.min(1, chroma / ColorUtil.CHROMA_SCALE_MAX);
|
|
39
|
+
if (amount < 0) {
|
|
40
|
+
const targetL2 = Math.max(0, lightness * (1 - ColorUtil.SHADOW_L_FACTOR * strength));
|
|
41
|
+
const targetH2 = ColorUtil.rotateHue(
|
|
42
|
+
hue,
|
|
43
|
+
ColorUtil.SHADOW_HUE_TARGET,
|
|
44
|
+
ColorUtil.SHADOW_HUE_PULL * strength * chromaScale
|
|
45
|
+
);
|
|
46
|
+
const targetC2 = chroma * (1 + ColorUtil.SHADOW_CHROMA_FACTOR * strength);
|
|
47
|
+
return OklabUtil.lchToHex([targetL2, targetC2, targetH2]);
|
|
48
|
+
}
|
|
49
|
+
const targetL = Math.min(1, lightness + (1 - lightness) * (ColorUtil.LIGHT_L_FACTOR * strength));
|
|
50
|
+
const targetH = ColorUtil.rotateHue(
|
|
51
|
+
hue,
|
|
52
|
+
ColorUtil.LIGHT_HUE_TARGET,
|
|
53
|
+
ColorUtil.LIGHT_HUE_PULL * strength * chromaScale
|
|
54
|
+
);
|
|
55
|
+
const targetC = chroma * (1 - ColorUtil.LIGHT_CHROMA_FACTOR * strength);
|
|
56
|
+
return OklabUtil.lchToHex([targetL, targetC, targetH]);
|
|
57
|
+
}
|
|
58
|
+
static rotateHue(current, target, weight) {
|
|
59
|
+
let diff = (target - current) % ColorUtil.FULL_TURN;
|
|
60
|
+
if (diff > ColorUtil.HALF_TURN) {
|
|
61
|
+
diff -= ColorUtil.FULL_TURN;
|
|
62
|
+
} else if (diff < -ColorUtil.HALF_TURN) {
|
|
63
|
+
diff += ColorUtil.FULL_TURN;
|
|
64
|
+
}
|
|
65
|
+
return (current + diff * weight + ColorUtil.FULL_TURN) % ColorUtil.FULL_TURN;
|
|
66
|
+
}
|
|
67
|
+
static toHexChannel(value) {
|
|
68
|
+
const clamped = Math.min(ColorUtil.CHANNEL_MAX, Math.max(0, value));
|
|
69
|
+
const raw = clamped.toString(ColorUtil.HEX_RADIX);
|
|
70
|
+
return raw.padStart(2, "0");
|
|
71
|
+
}
|
|
72
|
+
static mix(first, second, weight) {
|
|
73
|
+
const firstDigits = ColorUtil.expand(first.replace("#", ""));
|
|
74
|
+
const secondDigits = ColorUtil.expand(second.replace("#", ""));
|
|
75
|
+
if (!/^[0-9a-f]{6}$/i.test(firstDigits) || !/^[0-9a-f]{6}$/i.test(secondDigits)) {
|
|
76
|
+
return first;
|
|
77
|
+
}
|
|
78
|
+
const factor = Math.min(1, Math.max(0, weight));
|
|
79
|
+
const inverse = 1 - factor;
|
|
80
|
+
const [r1 = 0, g1 = 0, b1 = 0] = ColorUtil.channelsOf(firstDigits);
|
|
81
|
+
const [r2 = 0, g2 = 0, b2 = 0] = ColorUtil.channelsOf(secondDigits);
|
|
82
|
+
const red = Math.round(r1 * inverse + r2 * factor);
|
|
83
|
+
const green = Math.round(g1 * inverse + g2 * factor);
|
|
84
|
+
const blue = Math.round(b1 * inverse + b2 * factor);
|
|
85
|
+
return `#${ColorUtil.toHexChannel(red)}${ColorUtil.toHexChannel(green)}${ColorUtil.toHexChannel(blue)}`;
|
|
23
86
|
}
|
|
24
87
|
static expand(digits) {
|
|
25
88
|
return digits.length === 3 ? Array.from(digits, (char) => char + char).join("") : digits;
|