Skip to content
This repository was archived by the owner on Oct 7, 2021. It is now read-only.

Commit cf4c087

Browse files
committed
add background color handling
1 parent 286db62 commit cf4c087

2 files changed

Lines changed: 30 additions & 6 deletions

File tree

src/modules/setSplashScreen/ios/service.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import { addIosImageSetContents, EImageSetType } from '../../../services/ios/ser
22
import { generateResizedAssets } from '../../../services/image.processing';
33
import { config } from './config';
44
import { join } from 'path';
5-
import { replaceInFile, copyFile } from '../../../services/file.processing';
5+
import { replaceInFile } from '../../../services/file.processing';
6+
import { getNormalizedRGBAColors } from '../../../services/color.processing';
67
import { EResizeMode } from '../../../services/type';
78
import { getIosPackageName } from '../../../utils';
89

@@ -14,17 +15,24 @@ export const addIosSplashScreen = async (
1415
try {
1516
const iosSplashImageFolder = addIosImageSetContents('SplashImage', EImageSetType.IMAGE);
1617
await generateIosSplashImages(imageSource, iosSplashImageFolder);
17-
copyStoryBoardToProject();
18+
setBackgroundColorToStoryBoard(backgroundColor);
1819
setNewSplashScreenFileRefInInfoPlist();
1920
} catch (err) {
2021
console.log(err);
2122
}
2223
};
2324

24-
const copyStoryBoardToProject = () => {
25-
copyFile(
25+
const setBackgroundColorToStoryBoard = (backgroundColor: string) => {
26+
const { red, green, blue, alpha } = getNormalizedRGBAColors(backgroundColor);
27+
replaceInFile(
2628
join(__dirname, `../../../../templates/ios/SplashScreen.storyboard`),
27-
`./ios/${config.iosStoryboardName}.storyboard`
29+
`./ios/${config.iosStoryboardName}.storyboard`,
30+
[
31+
{
32+
oldContent: /<color.*key="backgroundColor".*\/>/g,
33+
newContent: `<color key="backgroundColor" red="${red}" green="${green}" blue="${blue}" alpha="${alpha}" colorSpace="custom" customColorSpace="sRGB"/>`,
34+
},
35+
]
2836
);
2937
};
3038

@@ -40,7 +48,7 @@ const setNewSplashScreenFileRefInInfoPlist = () => {
4048
};
4149

4250
const generateIosSplashImages = (imageSource: string, iosSplashImageFolder: string) => {
43-
const { multipliers, size, backgroundColor } = config.iosSplashImage;
51+
const { multipliers, size } = config.iosSplashImage;
4452
return Promise.all(
4553
multipliers.map(multiplier =>
4654
generateResizedAssets(

src/services/color.processing.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,21 @@
11
import colorParser from 'color-string';
22

3+
const BASE = 255;
4+
5+
export const getNormalizedRGBAColors = (color: string) => {
6+
try {
7+
const [red, green, blue, alpha] = colorParser.get.rgb(color);
8+
return {
9+
red: red / BASE,
10+
green: green / BASE,
11+
blue: blue / BASE,
12+
alpha,
13+
};
14+
} catch (err) {
15+
throw new Error('Could not parse your color');
16+
}
17+
};
18+
319
export const getHexColor = (color: string) => {
420
try {
521
const RGBA = colorParser.get.rgb(color);

0 commit comments

Comments
 (0)