Skip to content

Commit af81491

Browse files
authored
fix: icon generator color type error (#5838)
1 parent ca6fb68 commit af81491

File tree

1 file changed

+43
-38
lines changed

1 file changed

+43
-38
lines changed

lib/services/assets-generation/assets-generation-service.ts

+43-38
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Jimp, cssColorToHex, rgbaToInt, JimpInstance } from "jimp";
1+
import JimpModule = require("jimp");
22
import * as Color from "color";
33
import { exported } from "../../common/decorators";
44
import { AssetConstants } from "../../constants";
@@ -113,7 +113,7 @@ export class AssetsGenerationService implements IAssetsGenerationService {
113113
assetItem.data?.default ??
114114
"white";
115115

116-
const colorHEX: number = cssColorToHex(color);
116+
const colorHEX: number = JimpModule.cssColorToHex(color);
117117
const hex = colorHEX?.toString(16).substring(0, 6) ?? "FFFFFF";
118118

119119
this.$fs.writeFile(
@@ -164,7 +164,8 @@ export class AssetsGenerationService implements IAssetsGenerationService {
164164
continue;
165165
}
166166

167-
let image: JimpInstance;
167+
let image: JimpModule.JimpInstance;
168+
168169
switch (operation) {
169170
case Operations.OverlayWith:
170171
const overlayImageScale =
@@ -178,16 +179,10 @@ export class AssetsGenerationService implements IAssetsGenerationService {
178179
imageResize,
179180
imageResize,
180181
);
181-
image = this.generateImage(
182-
background,
183-
width,
184-
height,
185-
outputPath,
186-
image,
187-
);
182+
image = this.generateImage(background, width, height, image);
188183
break;
189184
case Operations.Blank:
190-
image = this.generateImage(background, width, height, outputPath);
185+
image = this.generateImage(background, width, height);
191186
break;
192187
case Operations.Resize:
193188
image = await this.resize(generationData.imagePath, width, height);
@@ -200,60 +195,56 @@ export class AssetsGenerationService implements IAssetsGenerationService {
200195
assetItem.height,
201196
);
202197
// The scale will apply to the underlying layer of the generated image
203-
image = this.generateImage(
204-
"#00000000",
205-
width,
206-
height,
207-
outputPath,
208-
image,
209-
);
198+
image = this.generateImage("#00000000", width, height, image);
210199
break;
211200
default:
212201
throw new Error(`Invalid image generation operation: ${operation}`);
213202
}
214203

215204
// This code disables the alpha chanel, as some images for the Apple App Store must not have transparency.
216205
if (assetItem.rgba === false) {
217-
//
218-
// The original code here became broken at some time and there is an issue posted here..
219-
// https://github.com/oliver-moran/jimp/issues/954
220-
// But NathanaelA recommended the below change and it works so maybe that's just what we go with.
221-
//
222-
// image = image.rgba(false);
223-
image = (image as any).colorType(2);
206+
// Add an underlying white layer
207+
image = this.generateImage("#FFFFFF", image.width, image.height, image);
224208
}
225209

226-
image.write(outputPath as any);
210+
if (this.isAssetFilePath(outputPath)) {
211+
image.write(outputPath);
212+
} else {
213+
this.$logger.warn(
214+
`Incorrect destination path ${outputPath} for image ${assetItem.filename}`,
215+
);
216+
}
227217
}
228218
}
229219

230220
private async resize(
231221
imagePath: string,
232222
width: number,
233223
height: number,
234-
): Promise<JimpInstance> {
235-
const image = await Jimp.read(imagePath);
224+
): Promise<JimpModule.JimpInstance> {
225+
const image = await JimpModule.Jimp.read(imagePath);
236226
return image.scaleToFit({
237227
w: width,
238228
h: height,
239-
}) as any;
229+
}) as JimpModule.JimpInstance;
240230
}
241231

242232
private generateImage(
243233
background: string,
244234
width: number,
245235
height: number,
246-
outputPath: string,
247-
overlayImage?: JimpInstance,
248-
): JimpInstance {
249-
// Typescript declarations for Jimp are not updated to define the constructor with backgroundColor so we workaround it by casting it to <any> for this case only.
250-
const J = <any>Jimp;
236+
overlayImage?: JimpModule.JimpInstance,
237+
): JimpModule.JimpInstance {
251238
const backgroundColor = this.getRgbaNumber(background);
252-
let image = new J(width, height, backgroundColor);
239+
let image = new JimpModule.Jimp({
240+
width,
241+
height,
242+
color: backgroundColor,
243+
});
253244

254245
if (overlayImage) {
255-
const centeredWidth = (width - overlayImage.bitmap.width) / 2;
256-
const centeredHeight = (height - overlayImage.bitmap.height) / 2;
246+
const centeredWidth = (width - overlayImage.width) / 2;
247+
const centeredHeight = (height - overlayImage.height) / 2;
257248
image = image.composite(overlayImage, centeredWidth, centeredHeight);
258249
}
259250

@@ -265,7 +256,21 @@ export class AssetsGenerationService implements IAssetsGenerationService {
265256
const colorRgb = color.rgb();
266257
const alpha = Math.round(colorRgb.alpha() * 255);
267258

268-
return rgbaToInt(colorRgb.red(), colorRgb.green(), colorRgb.blue(), alpha);
259+
return JimpModule.rgbaToInt(
260+
colorRgb.red(),
261+
colorRgb.green(),
262+
colorRgb.blue(),
263+
alpha,
264+
);
265+
}
266+
267+
private isAssetFilePath(path: string): path is `${string}.${string}` {
268+
if (!path) {
269+
return false;
270+
}
271+
272+
const index = path.lastIndexOf(".");
273+
return index > -1 && index < path.length - 1;
269274
}
270275
}
271276

0 commit comments

Comments
 (0)