@guilhermebkel/scenec 0.2.0 → 0.2.1
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 +22 -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
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
import { IdUtil } from "../Utils/IdUtil.js";
|
|
2
|
+
import { PixelUtil } from "../Utils/PixelUtil.js";
|
|
3
|
+
class ColorCritiqueService {
|
|
4
|
+
// Why: OKLab lightness below 0.33 represents deep dark tones.
|
|
5
|
+
static DARK_BAND_MAX = 0.33;
|
|
6
|
+
// Why: OKLab lightness above 0.67 represents light highlights and sky tones.
|
|
7
|
+
static LIGHT_BAND_MIN = 0.67;
|
|
8
|
+
// Why: when more than 85% of pixels fall into a single value band, the scene lacks value structure.
|
|
9
|
+
static MAX_SINGLE_BAND_SHARE = 0.85;
|
|
10
|
+
// Why: whiteout scenes with over 95% in highlights look washed out without dark anchors.
|
|
11
|
+
static MAX_LIGHT_BAND_SHARE = 0.95;
|
|
12
|
+
// Why: chroma below 0.04 represents neutral grays/whites/blacks which harmonize universally.
|
|
13
|
+
static NEUTRAL_CHROMA = 0.04;
|
|
14
|
+
// Why: chroma of 0.14 or higher represents vivid, saturated color capable of creating jarring harmony clashes.
|
|
15
|
+
static CLASH_CHROMA_THRESHOLD = 0.14;
|
|
16
|
+
// Why: analogous colors sit within 80 degrees on the color wheel.
|
|
17
|
+
static ANALOGOUS_MAX_DEG = 80;
|
|
18
|
+
// Why: complementary colors sit between 150 and 210 degrees on the color wheel.
|
|
19
|
+
static COMPLEMENTARY_MIN_DEG = 150;
|
|
20
|
+
static COMPLEMENTARY_MAX_DEG = 210;
|
|
21
|
+
// Why: minimum pixel count for an object's clashing color patch to be visually disruptive.
|
|
22
|
+
static MIN_CLASH_PIXELS = 15;
|
|
23
|
+
// Why: a hue family must carry at least 8% of the total chromatic weight to be a recognized palette pillar.
|
|
24
|
+
static MIN_DOMINANT_WEIGHT_SHARE = 0.08;
|
|
25
|
+
// Why: when placed objects cover under 5% of the frame, the scene is empty and color harmony checks are premature.
|
|
26
|
+
static MIN_SCENE_COVERAGE = 0.05;
|
|
27
|
+
// Why: 12 bins of 30 degrees each divide the color wheel into standard color families.
|
|
28
|
+
static HUE_BIN_COUNT = 12;
|
|
29
|
+
// Why: 360 degrees in a full color wheel circle.
|
|
30
|
+
static FULL_CIRCLE_DEG = 360;
|
|
31
|
+
// Why: half circle for wrap-around distance.
|
|
32
|
+
static HALF_CIRCLE_DEG = 180;
|
|
33
|
+
// Why: radix 16 for hexadecimal string conversions.
|
|
34
|
+
static HEX_RADIX = 16;
|
|
35
|
+
// Why: sky and floor environments don't represent placed objects capable of clashing with their own scene.
|
|
36
|
+
static BACKGROUND_KINDS = ["floor", "sky", "ceiling"];
|
|
37
|
+
// Why: RGBA channels and offsets.
|
|
38
|
+
static CHANNELS = 4;
|
|
39
|
+
static RED_OFFSET = 0;
|
|
40
|
+
static GREEN_OFFSET = 1;
|
|
41
|
+
static BLUE_OFFSET = 2;
|
|
42
|
+
static OPAQUE_ALPHA = 128;
|
|
43
|
+
// Why: LAB component offsets.
|
|
44
|
+
static LAB_L_OFFSET = 0;
|
|
45
|
+
static LAB_A_OFFSET = 1;
|
|
46
|
+
static LAB_B_OFFSET = 2;
|
|
47
|
+
static LAB_CHANNELS = 3;
|
|
48
|
+
static critique(raster) {
|
|
49
|
+
const totalObjectArea = raster.objects.reduce((sum, object) => sum + object.area, 0);
|
|
50
|
+
if (totalObjectArea / raster.pixelCount < ColorCritiqueService.MIN_SCENE_COVERAGE) {
|
|
51
|
+
return [];
|
|
52
|
+
}
|
|
53
|
+
return [
|
|
54
|
+
...ColorCritiqueService.checkValueStructure(raster),
|
|
55
|
+
...ColorCritiqueService.checkHarmony(raster)
|
|
56
|
+
];
|
|
57
|
+
}
|
|
58
|
+
static countValueBands(raster) {
|
|
59
|
+
let darkCount = 0;
|
|
60
|
+
let lightCount = 0;
|
|
61
|
+
let midCount = 0;
|
|
62
|
+
for (let index = 0; index < raster.pixelCount; index++) {
|
|
63
|
+
const offset = index * ColorCritiqueService.LAB_CHANNELS + ColorCritiqueService.LAB_L_OFFSET;
|
|
64
|
+
const lightness = raster.fullLab[offset] ?? 0;
|
|
65
|
+
if (lightness < ColorCritiqueService.DARK_BAND_MAX) {
|
|
66
|
+
darkCount++;
|
|
67
|
+
} else if (lightness > ColorCritiqueService.LIGHT_BAND_MIN) {
|
|
68
|
+
lightCount++;
|
|
69
|
+
} else {
|
|
70
|
+
midCount++;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
return {
|
|
74
|
+
darkShare: darkCount / raster.pixelCount,
|
|
75
|
+
lightShare: lightCount / raster.pixelCount,
|
|
76
|
+
midShare: midCount / raster.pixelCount
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
static checkValueStructure(raster) {
|
|
80
|
+
const { darkShare, lightShare, midShare } = ColorCritiqueService.countValueBands(raster);
|
|
81
|
+
const candidate = ColorCritiqueService.largestGroundObject(raster);
|
|
82
|
+
if (midShare >= ColorCritiqueService.MAX_SINGLE_BAND_SHARE) {
|
|
83
|
+
const target = candidate ? `'${candidate.id}' or the ground` : "the ground";
|
|
84
|
+
return darkShare > lightShare ? [`the picture has no lights; brighten ${target}`] : [`the picture has no darks; darken ${target}`];
|
|
85
|
+
}
|
|
86
|
+
if (lightShare >= ColorCritiqueService.MAX_LIGHT_BAND_SHARE) {
|
|
87
|
+
const target = candidate ? `'${candidate.id}' or the ground` : "the ground";
|
|
88
|
+
return [`the picture has no darks; darken ${target}`];
|
|
89
|
+
}
|
|
90
|
+
if (darkShare >= ColorCritiqueService.MAX_SINGLE_BAND_SHARE) {
|
|
91
|
+
const target = candidate ? `'${candidate.id}' or the sky` : "the sky";
|
|
92
|
+
return [`the picture has no lights; brighten ${target}`];
|
|
93
|
+
}
|
|
94
|
+
return [];
|
|
95
|
+
}
|
|
96
|
+
static checkHarmony(raster) {
|
|
97
|
+
const dominantHues = ColorCritiqueService.dominantHueClusters(raster);
|
|
98
|
+
if (dominantHues.length === 0) {
|
|
99
|
+
return [];
|
|
100
|
+
}
|
|
101
|
+
const messages = [];
|
|
102
|
+
for (const object of raster.objects) {
|
|
103
|
+
if (IdUtil.isOneOf(ColorCritiqueService.BACKGROUND_KINDS, object.kind)) {
|
|
104
|
+
continue;
|
|
105
|
+
}
|
|
106
|
+
const clash = ColorCritiqueService.clashOnObject(raster, object, dominantHues);
|
|
107
|
+
if (clash) {
|
|
108
|
+
messages.push(clash);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
return messages;
|
|
112
|
+
}
|
|
113
|
+
static dominantHueClusters(raster) {
|
|
114
|
+
const bins = new Float32Array(ColorCritiqueService.HUE_BIN_COUNT);
|
|
115
|
+
const binDegrees = ColorCritiqueService.FULL_CIRCLE_DEG / ColorCritiqueService.HUE_BIN_COUNT;
|
|
116
|
+
let totalChroma = 0;
|
|
117
|
+
for (let index = 0; index < raster.pixelCount; index++) {
|
|
118
|
+
const offset = index * ColorCritiqueService.LAB_CHANNELS;
|
|
119
|
+
const componentA = raster.fullLab[offset + ColorCritiqueService.LAB_A_OFFSET] ?? 0;
|
|
120
|
+
const componentB = raster.fullLab[offset + ColorCritiqueService.LAB_B_OFFSET] ?? 0;
|
|
121
|
+
const chroma = Math.hypot(componentA, componentB);
|
|
122
|
+
if (chroma >= ColorCritiqueService.NEUTRAL_CHROMA) {
|
|
123
|
+
const hue = ColorCritiqueService.hueOf(componentA, componentB);
|
|
124
|
+
const binIndex = Math.trunc(hue / binDegrees) % ColorCritiqueService.HUE_BIN_COUNT;
|
|
125
|
+
bins[binIndex] = (bins[binIndex] ?? 0) + chroma;
|
|
126
|
+
totalChroma += chroma;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
if (totalChroma === 0) {
|
|
130
|
+
return [];
|
|
131
|
+
}
|
|
132
|
+
const dominant = [];
|
|
133
|
+
bins.forEach((weight, binIndex) => {
|
|
134
|
+
if (weight / totalChroma >= ColorCritiqueService.MIN_DOMINANT_WEIGHT_SHARE) {
|
|
135
|
+
dominant.push(Math.round(binIndex * binDegrees + binDegrees / 2));
|
|
136
|
+
}
|
|
137
|
+
});
|
|
138
|
+
return dominant;
|
|
139
|
+
}
|
|
140
|
+
static closestDominant(hue, dominantHues) {
|
|
141
|
+
let minDistance = ColorCritiqueService.FULL_CIRCLE_DEG;
|
|
142
|
+
let nearHue = dominantHues[0] ?? 0;
|
|
143
|
+
for (const dominant of dominantHues) {
|
|
144
|
+
const distance = ColorCritiqueService.circularDistance(hue, dominant);
|
|
145
|
+
if (distance < minDistance) {
|
|
146
|
+
minDistance = distance;
|
|
147
|
+
nearHue = dominant;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
return { distance: minDistance, nearHue };
|
|
151
|
+
}
|
|
152
|
+
static isHarmonious(hue, dominantHues) {
|
|
153
|
+
return dominantHues.some((dominant) => {
|
|
154
|
+
const distance = ColorCritiqueService.circularDistance(hue, dominant);
|
|
155
|
+
const isAnalogous = distance <= ColorCritiqueService.ANALOGOUS_MAX_DEG;
|
|
156
|
+
const isComplementary = distance >= ColorCritiqueService.COMPLEMENTARY_MIN_DEG && distance <= ColorCritiqueService.COMPLEMENTARY_MAX_DEG;
|
|
157
|
+
return isAnalogous || isComplementary;
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
static clashOnObject(raster, object, dominantHues) {
|
|
161
|
+
const clashingPixels = [];
|
|
162
|
+
let bestDistance = ColorCritiqueService.FULL_CIRCLE_DEG;
|
|
163
|
+
let closestHue = dominantHues[0] ?? 0;
|
|
164
|
+
for (let index = 0; index < raster.pixelCount; index++) {
|
|
165
|
+
if (PixelUtil.read(object.alpha, index) <= ColorCritiqueService.OPAQUE_ALPHA) {
|
|
166
|
+
continue;
|
|
167
|
+
}
|
|
168
|
+
const offset = index * ColorCritiqueService.LAB_CHANNELS;
|
|
169
|
+
const componentA = raster.fullLab[offset + ColorCritiqueService.LAB_A_OFFSET] ?? 0;
|
|
170
|
+
const componentB = raster.fullLab[offset + ColorCritiqueService.LAB_B_OFFSET] ?? 0;
|
|
171
|
+
const chroma = Math.hypot(componentA, componentB);
|
|
172
|
+
if (chroma < ColorCritiqueService.CLASH_CHROMA_THRESHOLD) {
|
|
173
|
+
continue;
|
|
174
|
+
}
|
|
175
|
+
const hue = ColorCritiqueService.hueOf(componentA, componentB);
|
|
176
|
+
if (ColorCritiqueService.isHarmonious(hue, dominantHues)) {
|
|
177
|
+
continue;
|
|
178
|
+
}
|
|
179
|
+
clashingPixels.push(index);
|
|
180
|
+
const { distance, nearHue } = ColorCritiqueService.closestDominant(hue, dominantHues);
|
|
181
|
+
if (distance < bestDistance) {
|
|
182
|
+
bestDistance = distance;
|
|
183
|
+
closestHue = nearHue;
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
if (clashingPixels.length < ColorCritiqueService.MIN_CLASH_PIXELS) {
|
|
187
|
+
return null;
|
|
188
|
+
}
|
|
189
|
+
const hex = ColorCritiqueService.modalHex(raster, clashingPixels);
|
|
190
|
+
return `'${hex}' on '${object.id}' clashes with the palette; try a hue near ${closestHue}\xB0 or lower its saturation`;
|
|
191
|
+
}
|
|
192
|
+
static modalHex(raster, pixels) {
|
|
193
|
+
const hexToCount = /* @__PURE__ */ new Map();
|
|
194
|
+
for (const pixelIndex of pixels) {
|
|
195
|
+
const offset = pixelIndex * ColorCritiqueService.CHANNELS;
|
|
196
|
+
const red = PixelUtil.read(raster.rgba, offset + ColorCritiqueService.RED_OFFSET);
|
|
197
|
+
const green = PixelUtil.read(raster.rgba, offset + ColorCritiqueService.GREEN_OFFSET);
|
|
198
|
+
const blue = PixelUtil.read(raster.rgba, offset + ColorCritiqueService.BLUE_OFFSET);
|
|
199
|
+
const hex = `#${ColorCritiqueService.hexByte(red)}${ColorCritiqueService.hexByte(green)}${ColorCritiqueService.hexByte(blue)}`;
|
|
200
|
+
hexToCount.set(hex, (hexToCount.get(hex) ?? 0) + 1);
|
|
201
|
+
}
|
|
202
|
+
let bestHex = "#000000";
|
|
203
|
+
let highestCount = 0;
|
|
204
|
+
for (const [hex, count] of hexToCount.entries()) {
|
|
205
|
+
if (count > highestCount) {
|
|
206
|
+
highestCount = count;
|
|
207
|
+
bestHex = hex;
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
return bestHex;
|
|
211
|
+
}
|
|
212
|
+
static hexByte(value) {
|
|
213
|
+
return value.toString(ColorCritiqueService.HEX_RADIX).padStart(2, "0").toUpperCase();
|
|
214
|
+
}
|
|
215
|
+
static hueOf(componentA, componentB) {
|
|
216
|
+
const degrees = Math.atan2(componentB, componentA) * ColorCritiqueService.HALF_CIRCLE_DEG / Math.PI;
|
|
217
|
+
return (degrees + ColorCritiqueService.FULL_CIRCLE_DEG) % ColorCritiqueService.FULL_CIRCLE_DEG;
|
|
218
|
+
}
|
|
219
|
+
static circularDistance(first, second) {
|
|
220
|
+
const diff = Math.abs(first - second) % ColorCritiqueService.FULL_CIRCLE_DEG;
|
|
221
|
+
return diff > ColorCritiqueService.HALF_CIRCLE_DEG ? ColorCritiqueService.FULL_CIRCLE_DEG - diff : diff;
|
|
222
|
+
}
|
|
223
|
+
static largestGroundObject(raster) {
|
|
224
|
+
const candidates = raster.objects.filter(
|
|
225
|
+
(candidate) => !IdUtil.isOneOf(ColorCritiqueService.BACKGROUND_KINDS, candidate.kind)
|
|
226
|
+
);
|
|
227
|
+
return candidates.toSorted((first, second) => second.area - first.area)[0];
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
export {
|
|
231
|
+
ColorCritiqueService
|
|
232
|
+
};
|
|
@@ -5,6 +5,7 @@ import { IdUtil } from "../Utils/IdUtil.js";
|
|
|
5
5
|
import { AssetService } from "./AssetService.js";
|
|
6
6
|
import { CustomAssetService } from "./CustomAssetService.js";
|
|
7
7
|
import { KitService } from "./KitService.js";
|
|
8
|
+
import { PaletteService } from "./PaletteService.js";
|
|
8
9
|
import { ThemeService } from "./ThemeService.js";
|
|
9
10
|
class ContextService {
|
|
10
11
|
static DEFAULT_THEME = "flat-cool";
|
|
@@ -39,14 +40,17 @@ class ContextService {
|
|
|
39
40
|
const unit = height * ContextService.UNIT_PER_HEIGHT * (canvas.scale ?? 1);
|
|
40
41
|
return { canvas, kit, idToObject: {}, library, failedAssets, theme, warnings, width, height, setting, lines, unit };
|
|
41
42
|
}
|
|
42
|
-
static themeOf(
|
|
43
|
-
if (
|
|
43
|
+
static themeOf(themeOrName, warnings) {
|
|
44
|
+
if (themeOrName === void 0) {
|
|
44
45
|
return ThemeService.THEME_ID_TO_THEME[ContextService.DEFAULT_THEME];
|
|
45
46
|
}
|
|
46
|
-
if (
|
|
47
|
-
return
|
|
47
|
+
if (typeof themeOrName === "object") {
|
|
48
|
+
return PaletteService.generate(themeOrName);
|
|
49
|
+
}
|
|
50
|
+
if (IdUtil.isOneOf(THEME_IDS, themeOrName)) {
|
|
51
|
+
return ThemeService.THEME_ID_TO_THEME[themeOrName];
|
|
48
52
|
}
|
|
49
|
-
warnings.push(`theme '${
|
|
53
|
+
warnings.push(`theme '${themeOrName}' does not exist; using '${ContextService.DEFAULT_THEME}'. Available: ${THEME_IDS.join(", ")}`);
|
|
50
54
|
return ThemeService.THEME_ID_TO_THEME[ContextService.DEFAULT_THEME];
|
|
51
55
|
}
|
|
52
56
|
static kitOf(name, warnings) {
|
|
@@ -4,6 +4,7 @@ export declare class DescriptionService {
|
|
|
4
4
|
private static readonly PERCENT;
|
|
5
5
|
private static percentOf;
|
|
6
6
|
private static roundedPercentOf;
|
|
7
|
-
static
|
|
7
|
+
private static readonly FRAMING_NAMES;
|
|
8
|
+
static describe(ordered: Placement[], context: CompilationContext, framingCorner?: string): string;
|
|
8
9
|
static layout(ordered: Placement[], context: CompilationContext): LayoutEntry[];
|
|
9
10
|
}
|
|
@@ -6,9 +6,15 @@ class DescriptionService {
|
|
|
6
6
|
static roundedPercentOf(value, total) {
|
|
7
7
|
return Number((value / total * DescriptionService.PERCENT).toFixed(1));
|
|
8
8
|
}
|
|
9
|
-
static
|
|
9
|
+
static FRAMING_NAMES = /* @__PURE__ */ new Set(["foliage", "curtain", "plants"]);
|
|
10
|
+
static describe(ordered, context, framingCorner) {
|
|
10
11
|
const { width, height } = context;
|
|
11
|
-
|
|
12
|
+
const lines = ordered.map((placement) => `${placement.id} (${placement.asset}): x=${DescriptionService.percentOf(placement.x, width)}%, base=${DescriptionService.percentOf(placement.y, height)}%, ${DescriptionService.percentOf(placement.width, width)}%\xD7${DescriptionService.percentOf(placement.height, height)}% of the frame${placement.depth ? `, ${placement.depth}` : ""}`);
|
|
13
|
+
const framingKind = context.canvas.framing ?? context.canvas.frame_elements;
|
|
14
|
+
if (framingCorner && framingKind && DescriptionService.FRAMING_NAMES.has(framingKind)) {
|
|
15
|
+
lines.push(`framing (${framingKind}): foreground framing in the ${framingCorner} corner`);
|
|
16
|
+
}
|
|
17
|
+
return lines.join("\n");
|
|
12
18
|
}
|
|
13
19
|
static layout(ordered, context) {
|
|
14
20
|
const { width, height } = context;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { DetailName } from "../Protocols/AssetProtocol.js";
|
|
2
|
+
import type { CompilationContext } from "../Protocols/CompilationProtocol.js";
|
|
3
|
+
import type { SceneObject } from "../Protocols/SceneProtocol.js";
|
|
4
|
+
export declare class DetailService {
|
|
5
|
+
private static readonly DEPTH_TO_DETAIL;
|
|
6
|
+
static resolve(id: string, object: SceneObject, context: CompilationContext): DetailName;
|
|
7
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { DetailUtil } from "../Utils/DetailUtil.js";
|
|
2
|
+
import { IdUtil } from "../Utils/IdUtil.js";
|
|
3
|
+
class DetailService {
|
|
4
|
+
static DEPTH_TO_DETAIL = { far: "low" };
|
|
5
|
+
static resolve(id, object, context) {
|
|
6
|
+
if (DetailUtil.isDetail(object.detail)) {
|
|
7
|
+
return object.detail;
|
|
8
|
+
}
|
|
9
|
+
const focus = [].concat(context.canvas.focus ?? []);
|
|
10
|
+
if (focus.some((focused) => id === focused || id.startsWith(`${focused}_`))) {
|
|
11
|
+
return "high";
|
|
12
|
+
}
|
|
13
|
+
return IdUtil.lookup(DetailService.DEPTH_TO_DETAIL, object.depth) ?? "mid";
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
export {
|
|
17
|
+
DetailService
|
|
18
|
+
};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { Raster } from "../Protocols/CritiqueProtocol.js";
|
|
2
|
+
export declare class FocusCritiqueService {
|
|
3
|
+
private static readonly OPAQUE_ALPHA;
|
|
4
|
+
private static readonly SURROUND_DILATION_PX;
|
|
5
|
+
private static readonly MIN_AREA;
|
|
6
|
+
private static readonly CHROMA_MARGIN;
|
|
7
|
+
private static readonly MIN_PROMINENT_CHROMA;
|
|
8
|
+
private static readonly CONTRAST_MARGIN;
|
|
9
|
+
private static readonly MAX_FOCUS_CONTRAST_TO_WARN;
|
|
10
|
+
private static readonly HIGH_CHROMA_THRESHOLD;
|
|
11
|
+
private static readonly MAX_SATURATION_AREA_SHARE;
|
|
12
|
+
private static readonly LAB_A_OFFSET;
|
|
13
|
+
private static readonly LAB_B_OFFSET;
|
|
14
|
+
private static readonly LAB_CHANNELS;
|
|
15
|
+
static critique(raster: Raster, focusId?: string): string[];
|
|
16
|
+
private static isIgnoredCandidate;
|
|
17
|
+
private static findFocus;
|
|
18
|
+
private static metricsOf;
|
|
19
|
+
private static contrastAgainstSurround;
|
|
20
|
+
private static compare;
|
|
21
|
+
private static saturationBudget;
|
|
22
|
+
private static dilate;
|
|
23
|
+
}
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
import { PixelUtil } from "../Utils/PixelUtil.js";
|
|
2
|
+
class FocusCritiqueService {
|
|
3
|
+
// Why: alpha above 128 marks the solid perceived shape of an SVG element.
|
|
4
|
+
static OPAQUE_ALPHA = 128;
|
|
5
|
+
// Why: dilating by 3 px isolates the immediate background ring surrounding an object silhouette.
|
|
6
|
+
static SURROUND_DILATION_PX = 3;
|
|
7
|
+
// Why: objects smaller than 30 px are minor decorative props that do not establish focal dominance.
|
|
8
|
+
static MIN_AREA = 30;
|
|
9
|
+
// Why: a non-focus element with chroma exceeding the focus by 0.08 draws attention away from the story focus.
|
|
10
|
+
static CHROMA_MARGIN = 0.08;
|
|
11
|
+
// Why: chroma below 0.16 is muted and cannot overpower a focal subject.
|
|
12
|
+
static MIN_PROMINENT_CHROMA = 0.16;
|
|
13
|
+
// Why: a contrast difference of 0.15 lightness units creates a noticeably stronger visual pop.
|
|
14
|
+
static CONTRAST_MARGIN = 0.15;
|
|
15
|
+
// Why: a focus that already has high local contrast (0.28) remains distinct even if other objects also contrast.
|
|
16
|
+
static MAX_FOCUS_CONTRAST_TO_WARN = 0.28;
|
|
17
|
+
// Why: chroma at or above 0.15 in OKLab represents vivid, intense color.
|
|
18
|
+
static HIGH_CHROMA_THRESHOLD = 0.15;
|
|
19
|
+
// Why: when more than 30% of the image has vivid saturation, the viewer suffers visual fatigue and hierarchy collapses.
|
|
20
|
+
static MAX_SATURATION_AREA_SHARE = 0.3;
|
|
21
|
+
// Why: LAB component index offsets for lightness, a and b channels.
|
|
22
|
+
static LAB_A_OFFSET = 1;
|
|
23
|
+
static LAB_B_OFFSET = 2;
|
|
24
|
+
static LAB_CHANNELS = 3;
|
|
25
|
+
static critique(raster, focusId) {
|
|
26
|
+
const messages = [];
|
|
27
|
+
const budgetWarning = FocusCritiqueService.saturationBudget(raster);
|
|
28
|
+
if (budgetWarning) {
|
|
29
|
+
messages.push(budgetWarning);
|
|
30
|
+
}
|
|
31
|
+
const focusObject = FocusCritiqueService.findFocus(raster, focusId);
|
|
32
|
+
if (!focusObject) {
|
|
33
|
+
return messages;
|
|
34
|
+
}
|
|
35
|
+
const candidates = raster.objects.filter(
|
|
36
|
+
(candidate) => candidate.area >= FocusCritiqueService.MIN_AREA && candidate.kind !== "floor"
|
|
37
|
+
);
|
|
38
|
+
const focusMetrics = FocusCritiqueService.metricsOf(raster, focusObject);
|
|
39
|
+
for (const candidate of candidates) {
|
|
40
|
+
if (FocusCritiqueService.isIgnoredCandidate(candidate, focusObject)) {
|
|
41
|
+
continue;
|
|
42
|
+
}
|
|
43
|
+
const otherMetrics = FocusCritiqueService.metricsOf(raster, candidate);
|
|
44
|
+
const comparison = FocusCritiqueService.compare(focusMetrics, otherMetrics);
|
|
45
|
+
if (comparison) {
|
|
46
|
+
messages.push(comparison);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
return messages;
|
|
50
|
+
}
|
|
51
|
+
static isIgnoredCandidate(candidate, focus) {
|
|
52
|
+
if (candidate.id === focus.id || candidate.related.includes(focus.id) || focus.related.includes(candidate.id)) {
|
|
53
|
+
return true;
|
|
54
|
+
}
|
|
55
|
+
const candidatePrefix = candidate.id.includes("_") ? candidate.id.slice(0, candidate.id.indexOf("_")) : null;
|
|
56
|
+
const focusPrefix = focus.id.includes("_") ? focus.id.slice(0, focus.id.indexOf("_")) : null;
|
|
57
|
+
return candidatePrefix !== null && candidatePrefix === focusPrefix;
|
|
58
|
+
}
|
|
59
|
+
static findFocus(raster, focusId) {
|
|
60
|
+
const targetId = focusId ?? raster.focusId;
|
|
61
|
+
if (targetId) {
|
|
62
|
+
const match = raster.objects.find((candidate) => candidate.id === targetId || candidate.id.startsWith(`${targetId}_`));
|
|
63
|
+
if (match) {
|
|
64
|
+
return match;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
const flagged = raster.objects.find((candidate) => candidate.isFocus);
|
|
68
|
+
if (flagged) {
|
|
69
|
+
return flagged;
|
|
70
|
+
}
|
|
71
|
+
const people = raster.objects.filter(
|
|
72
|
+
(candidate) => candidate.asset === "person" && candidate.area >= FocusCritiqueService.MIN_AREA
|
|
73
|
+
);
|
|
74
|
+
return people.toSorted((first, second) => second.area - first.area)[0];
|
|
75
|
+
}
|
|
76
|
+
static metricsOf(raster, object) {
|
|
77
|
+
let sumL = 0;
|
|
78
|
+
let sumChroma = 0;
|
|
79
|
+
let count = 0;
|
|
80
|
+
for (let index = 0; index < raster.pixelCount; index++) {
|
|
81
|
+
if (PixelUtil.read(object.alpha, index) > FocusCritiqueService.OPAQUE_ALPHA) {
|
|
82
|
+
const offset = index * FocusCritiqueService.LAB_CHANNELS;
|
|
83
|
+
const lightness = raster.fullLab[offset] ?? 0;
|
|
84
|
+
const componentA = raster.fullLab[offset + FocusCritiqueService.LAB_A_OFFSET] ?? 0;
|
|
85
|
+
const componentB = raster.fullLab[offset + FocusCritiqueService.LAB_B_OFFSET] ?? 0;
|
|
86
|
+
sumL += lightness;
|
|
87
|
+
sumChroma += Math.hypot(componentA, componentB);
|
|
88
|
+
count++;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
const meanL = count > 0 ? sumL / count : 0;
|
|
92
|
+
const meanChroma = count > 0 ? sumChroma / count : 0;
|
|
93
|
+
const localContrast = FocusCritiqueService.contrastAgainstSurround(raster, object, meanL);
|
|
94
|
+
return { object, meanChroma, localContrast };
|
|
95
|
+
}
|
|
96
|
+
static contrastAgainstSurround(raster, object, objectL) {
|
|
97
|
+
const dilated = FocusCritiqueService.dilate(raster, object);
|
|
98
|
+
let sumSurroundL = 0;
|
|
99
|
+
let surroundCount = 0;
|
|
100
|
+
for (let index = 0; index < raster.pixelCount; index++) {
|
|
101
|
+
if (dilated[index] === 1 && PixelUtil.read(object.alpha, index) <= FocusCritiqueService.OPAQUE_ALPHA) {
|
|
102
|
+
const offset = index * FocusCritiqueService.LAB_CHANNELS;
|
|
103
|
+
sumSurroundL += raster.fullLab[offset] ?? 0;
|
|
104
|
+
surroundCount++;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
if (surroundCount === 0) {
|
|
108
|
+
return 0;
|
|
109
|
+
}
|
|
110
|
+
const meanSurroundL = sumSurroundL / surroundCount;
|
|
111
|
+
return Math.abs(objectL - meanSurroundL);
|
|
112
|
+
}
|
|
113
|
+
static compare(focus, other) {
|
|
114
|
+
const isChromaHigher = other.meanChroma > focus.meanChroma + FocusCritiqueService.CHROMA_MARGIN && other.meanChroma >= FocusCritiqueService.MIN_PROMINENT_CHROMA;
|
|
115
|
+
if (isChromaHigher) {
|
|
116
|
+
const otherValue = other.meanChroma.toFixed(2);
|
|
117
|
+
const focusValue = focus.meanChroma.toFixed(2);
|
|
118
|
+
return `'${other.object.id}' draws more attention than the focus '${focus.object.id}' (chroma ${otherValue} vs ${focusValue}); make '${other.object.id}' less saturated or the focus brighter`;
|
|
119
|
+
}
|
|
120
|
+
const isContrastHigher = other.localContrast > focus.localContrast + FocusCritiqueService.CONTRAST_MARGIN && focus.localContrast < FocusCritiqueService.MAX_FOCUS_CONTRAST_TO_WARN;
|
|
121
|
+
if (isContrastHigher) {
|
|
122
|
+
const otherValue = other.localContrast.toFixed(2);
|
|
123
|
+
const focusValue = focus.localContrast.toFixed(2);
|
|
124
|
+
return `'${other.object.id}' draws more attention than the focus '${focus.object.id}' (contrast ${otherValue} vs ${focusValue}); make '${other.object.id}' less saturated or the focus brighter`;
|
|
125
|
+
}
|
|
126
|
+
return null;
|
|
127
|
+
}
|
|
128
|
+
static saturationBudget(raster) {
|
|
129
|
+
let saturatedCount = 0;
|
|
130
|
+
for (let index = 0; index < raster.pixelCount; index++) {
|
|
131
|
+
const offset = index * FocusCritiqueService.LAB_CHANNELS;
|
|
132
|
+
const componentA = raster.fullLab[offset + FocusCritiqueService.LAB_A_OFFSET] ?? 0;
|
|
133
|
+
const componentB = raster.fullLab[offset + FocusCritiqueService.LAB_B_OFFSET] ?? 0;
|
|
134
|
+
if (Math.hypot(componentA, componentB) >= FocusCritiqueService.HIGH_CHROMA_THRESHOLD) {
|
|
135
|
+
saturatedCount++;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
const share = saturatedCount / raster.pixelCount;
|
|
139
|
+
if (share > FocusCritiqueService.MAX_SATURATION_AREA_SHARE) {
|
|
140
|
+
const percent = Math.round(share * 100);
|
|
141
|
+
return `too much saturated color (${percent}% of the frame); reduce saturation on large background areas`;
|
|
142
|
+
}
|
|
143
|
+
return null;
|
|
144
|
+
}
|
|
145
|
+
static dilate(raster, object) {
|
|
146
|
+
const dilated = new Uint8Array(raster.pixelCount);
|
|
147
|
+
const reach = FocusCritiqueService.SURROUND_DILATION_PX;
|
|
148
|
+
for (let index = 0; index < raster.pixelCount; index++) {
|
|
149
|
+
if (PixelUtil.read(object.alpha, index) > FocusCritiqueService.OPAQUE_ALPHA) {
|
|
150
|
+
const centerRow = Math.trunc(index / raster.width);
|
|
151
|
+
const centerCol = index % raster.width;
|
|
152
|
+
const firstRow = Math.max(0, centerRow - reach);
|
|
153
|
+
const lastRow = Math.min(raster.height - 1, centerRow + reach);
|
|
154
|
+
const firstCol = Math.max(0, centerCol - reach);
|
|
155
|
+
const lastCol = Math.min(raster.width - 1, centerCol + reach);
|
|
156
|
+
for (let row = firstRow; row <= lastRow; row++) {
|
|
157
|
+
dilated.fill(1, row * raster.width + firstCol, row * raster.width + lastCol + 1);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
return dilated;
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
export {
|
|
165
|
+
FocusCritiqueService
|
|
166
|
+
};
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { CompilationContext, Placement, TextRect } from "../Protocols/CompilationProtocol.js";
|
|
2
|
+
import type { FramingElement } from "../Protocols/SceneProtocol.js";
|
|
3
|
+
export type FramingCorner = "top-left" | "top-right" | "bottom-left" | "bottom-right";
|
|
4
|
+
export interface FramingResult {
|
|
5
|
+
svg: string;
|
|
6
|
+
kind: FramingElement;
|
|
7
|
+
corner?: FramingCorner;
|
|
8
|
+
}
|
|
9
|
+
export declare class FramingService {
|
|
10
|
+
private static readonly BLUR_SIGMA;
|
|
11
|
+
private static readonly DEFAULT_FOCUS_X;
|
|
12
|
+
private static readonly INK_SHADE;
|
|
13
|
+
private static readonly CORNER_WIDTH_RATIO;
|
|
14
|
+
private static readonly CORNER_HEIGHT_RATIO;
|
|
15
|
+
private static readonly CURTAIN_HEIGHT_RATIO;
|
|
16
|
+
private static readonly CURTAIN_BOTTOM_RATIO;
|
|
17
|
+
private static readonly FRAME_LEFT_RATIO;
|
|
18
|
+
private static readonly FRAME_RIGHT_RATIO;
|
|
19
|
+
private static readonly TOP_KINDS;
|
|
20
|
+
private static readonly FRAME_TO_X_RATIO;
|
|
21
|
+
private static readonly KIND_TO_DRAWER;
|
|
22
|
+
static render(context: CompilationContext, placements: readonly Placement[], textRects: readonly TextRect[]): FramingResult;
|
|
23
|
+
private static resolveFocusX;
|
|
24
|
+
private static selectCorner;
|
|
25
|
+
private static chooseTopCorner;
|
|
26
|
+
private static chooseBottomCorner;
|
|
27
|
+
private static collidesWithText;
|
|
28
|
+
private static drawFoliage;
|
|
29
|
+
private static drawCurtain;
|
|
30
|
+
private static drawPlants;
|
|
31
|
+
}
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import { ColorUtil } from "../Utils/ColorUtil.js";
|
|
2
|
+
import { IdUtil } from "../Utils/IdUtil.js";
|
|
3
|
+
class FramingService {
|
|
4
|
+
static BLUR_SIGMA = "3.2";
|
|
5
|
+
static DEFAULT_FOCUS_X = 0.5;
|
|
6
|
+
static INK_SHADE = -0.05;
|
|
7
|
+
static CORNER_WIDTH_RATIO = 0.28;
|
|
8
|
+
static CORNER_HEIGHT_RATIO = 0.32;
|
|
9
|
+
static CURTAIN_HEIGHT_RATIO = 0.85;
|
|
10
|
+
static CURTAIN_BOTTOM_RATIO = 0.95;
|
|
11
|
+
static FRAME_LEFT_RATIO = 0.3;
|
|
12
|
+
static FRAME_RIGHT_RATIO = 0.7;
|
|
13
|
+
static TOP_KINDS = ["foliage", "curtain"];
|
|
14
|
+
static FRAME_TO_X_RATIO = {
|
|
15
|
+
left: FramingService.FRAME_LEFT_RATIO,
|
|
16
|
+
center: FramingService.DEFAULT_FOCUS_X,
|
|
17
|
+
right: FramingService.FRAME_RIGHT_RATIO
|
|
18
|
+
};
|
|
19
|
+
static KIND_TO_DRAWER = {
|
|
20
|
+
foliage: (corner, width, height, ink) => FramingService.drawFoliage(corner, width, height, ink),
|
|
21
|
+
curtain: (corner, width, height, ink) => FramingService.drawCurtain(corner, width, height, ink),
|
|
22
|
+
plants: (corner, width, height, ink) => FramingService.drawPlants(corner, width, height, ink)
|
|
23
|
+
};
|
|
24
|
+
static render(context, placements, textRects) {
|
|
25
|
+
const rawKind = context.canvas.framing ?? context.canvas.frame_elements ?? "none";
|
|
26
|
+
const drawer = IdUtil.lookup(FramingService.KIND_TO_DRAWER, rawKind);
|
|
27
|
+
if (!drawer) {
|
|
28
|
+
return { svg: "", kind: "none" };
|
|
29
|
+
}
|
|
30
|
+
const focusX = FramingService.resolveFocusX(context, placements);
|
|
31
|
+
const isTopKind = IdUtil.isOneOf(FramingService.TOP_KINDS, rawKind);
|
|
32
|
+
const isLeftPreferred = focusX > context.width * FramingService.DEFAULT_FOCUS_X;
|
|
33
|
+
const corner = FramingService.selectCorner({
|
|
34
|
+
isTopKind,
|
|
35
|
+
isLeftPreferred,
|
|
36
|
+
width: context.width,
|
|
37
|
+
height: context.height,
|
|
38
|
+
textRects
|
|
39
|
+
});
|
|
40
|
+
const ink = ColorUtil.sh(context.theme.ink, FramingService.INK_SHADE);
|
|
41
|
+
const filterDef = `<defs><filter id="frameBlur" x="-20%" y="-20%" width="140%" height="140%"><feGaussianBlur stdDeviation="${FramingService.BLUR_SIGMA}"/></filter></defs>`;
|
|
42
|
+
const body = drawer(corner, context.width, context.height, ink);
|
|
43
|
+
const svg = `${filterDef}<g id="frame-repoussoir" filter="url(#frameBlur)">${body}</g>`;
|
|
44
|
+
return { svg, kind: rawKind, corner };
|
|
45
|
+
}
|
|
46
|
+
static resolveFocusX(context, placements) {
|
|
47
|
+
const focusPlacement = placements.find((candidate) => candidate.isFocus);
|
|
48
|
+
if (focusPlacement) {
|
|
49
|
+
return focusPlacement.x;
|
|
50
|
+
}
|
|
51
|
+
const frame = context.canvas.frame;
|
|
52
|
+
if (typeof frame === "number") {
|
|
53
|
+
return frame * context.width;
|
|
54
|
+
}
|
|
55
|
+
const namedRatio = frame ? IdUtil.lookup(FramingService.FRAME_TO_X_RATIO, frame) : void 0;
|
|
56
|
+
if (namedRatio != null) {
|
|
57
|
+
return namedRatio * context.width;
|
|
58
|
+
}
|
|
59
|
+
return context.width * FramingService.DEFAULT_FOCUS_X;
|
|
60
|
+
}
|
|
61
|
+
static selectCorner(parts) {
|
|
62
|
+
const { isTopKind, isLeftPreferred, width, height, textRects } = parts;
|
|
63
|
+
const choose = (isLeft) => isTopKind ? FramingService.chooseTopCorner(isLeft) : FramingService.chooseBottomCorner(isLeft);
|
|
64
|
+
const primary = choose(isLeftPreferred);
|
|
65
|
+
const alternative = choose(!isLeftPreferred);
|
|
66
|
+
if (!FramingService.collidesWithText(primary, width, height, textRects)) {
|
|
67
|
+
return primary;
|
|
68
|
+
}
|
|
69
|
+
return alternative;
|
|
70
|
+
}
|
|
71
|
+
static chooseTopCorner(isLeft) {
|
|
72
|
+
return isLeft ? "top-left" : "top-right";
|
|
73
|
+
}
|
|
74
|
+
static chooseBottomCorner(isLeft) {
|
|
75
|
+
return isLeft ? "bottom-left" : "bottom-right";
|
|
76
|
+
}
|
|
77
|
+
static collidesWithText(corner, width, height, textRects) {
|
|
78
|
+
const cornerWidth = width * FramingService.CORNER_WIDTH_RATIO;
|
|
79
|
+
const cornerHeight = height * FramingService.CORNER_HEIGHT_RATIO;
|
|
80
|
+
const box = {
|
|
81
|
+
x0: corner.includes("left") ? 0 : width - cornerWidth,
|
|
82
|
+
y0: corner.includes("top") ? 0 : height - cornerHeight,
|
|
83
|
+
x1: corner.includes("left") ? cornerWidth : width,
|
|
84
|
+
y1: corner.includes("top") ? cornerHeight : height
|
|
85
|
+
};
|
|
86
|
+
return textRects.some((rect) => rect.x0 < box.x1 && rect.x1 > box.x0 && rect.y0 < box.y1 && rect.y1 > box.y0);
|
|
87
|
+
}
|
|
88
|
+
static drawFoliage(corner, width, height, ink) {
|
|
89
|
+
const isLeft = corner.includes("left");
|
|
90
|
+
const flip = isLeft ? "" : ` transform="translate(${width} 0) scale(-1 1)"`;
|
|
91
|
+
return `<g${flip} fill="${ink}"><path d="M 0 0 Q 80 40 180 30 Q 140 100 90 120 Q 30 130 0 160 Z"/><path d="M 60 20 C 130 10 200 60 230 120 C 170 140 110 100 60 20 Z"/><path d="M 120 25 C 190 20 280 80 290 150 C 230 160 170 110 120 25 Z"/><path d="M 20 60 C 60 120 90 210 80 270 C 40 230 10 170 20 60 Z"/><path d="M 70 90 C 130 160 170 250 160 300 C 120 270 80 200 70 90 Z"/></g>`;
|
|
92
|
+
}
|
|
93
|
+
static drawCurtain(corner, width, height, ink) {
|
|
94
|
+
const isLeft = corner.includes("left");
|
|
95
|
+
const flip = isLeft ? "" : ` transform="translate(${width} 0) scale(-1 1)"`;
|
|
96
|
+
const targetHeight = height * FramingService.CURTAIN_HEIGHT_RATIO;
|
|
97
|
+
const bottomHeight = height * FramingService.CURTAIN_BOTTOM_RATIO;
|
|
98
|
+
return `<g${flip} fill="${ink}" opacity="0.92"><path d="M 0 0 L 160 0 C 140 100 130 200 110 320 C 90 440 60 ${targetHeight.toFixed(1)} 0 ${bottomHeight.toFixed(1)} Z"/><path d="M 40 0 C 35 120 25 240 15 360 C 10 420 5 480 0 540 L 0 0 Z" opacity="0.4"/></g>`;
|
|
99
|
+
}
|
|
100
|
+
static drawPlants(corner, width, height, ink) {
|
|
101
|
+
const isLeft = corner.includes("left");
|
|
102
|
+
const flip = isLeft ? ` transform="translate(0 ${height}) scale(1 -1)"` : ` transform="translate(${width} ${height}) scale(-1 -1)"`;
|
|
103
|
+
return `<g${flip} fill="${ink}"><path d="M 0 0 C 40 80 80 200 70 320 C 40 250 10 160 0 80 Z"/><path d="M 20 0 C 80 110 160 240 180 360 C 130 300 80 190 20 0 Z"/><path d="M 50 0 C 130 100 230 180 320 230 C 240 180 150 110 50 0 Z"/><path d="M 40 0 C 100 60 190 120 260 130 C 190 100 120 50 40 0 Z"/></g>`;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
export {
|
|
107
|
+
FramingService
|
|
108
|
+
};
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { DetailService } from "./DetailService.js";
|
|
1
2
|
import { IdUtil } from "../Utils/IdUtil.js";
|
|
2
3
|
class GazeService {
|
|
3
4
|
static DIRECTION_NAMES = [
|
|
@@ -30,7 +31,8 @@ class GazeService {
|
|
|
30
31
|
const deltaX = target.x - placement.x;
|
|
31
32
|
const deltaY = target.y - target.height / 2 - (placement.y - placement.height * GazeService.EYE_HEIGHT_FRACTION);
|
|
32
33
|
const length = Math.hypot(deltaX, deltaY) || 1;
|
|
33
|
-
const
|
|
34
|
+
const detail = DetailService.resolve(placement.id, object, context);
|
|
35
|
+
const params = { ...object, detail, gaze: [deltaX / length, deltaY / length] };
|
|
34
36
|
const drawing = library.person?.draw(params, theme);
|
|
35
37
|
if (drawing) {
|
|
36
38
|
placement.svg = drawing.svg;
|