Skip to content

Commit

Permalink
Merge pull request fluttercommunity#450 from HaoCherHong/feature/ios-…
Browse files Browse the repository at this point in the history
…icon-bg-color

support ios icon background color when removing alpha
  • Loading branch information
RatakondalaArun committed May 7, 2023
2 parents 45bfbfa + 0d2cc40 commit 88ab71c
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/config/config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class Config {
this.adaptiveIconBackground,
this.minSdkAndroid = constants.androidDefaultAndroidMinSDK,
this.removeAlphaIOS = false,
this.backgroundColorIOS = '#ffffff',
this.webConfig,
this.windowsConfig,
this.macOSConfig,
Expand Down Expand Up @@ -131,6 +132,10 @@ class Config {
@JsonKey(name: 'remove_alpha_ios')
final bool removeAlphaIOS;

/// IOS background_color_ios
@JsonKey(name: 'background_color_ios')
final String backgroundColorIOS;

/// Web platform config
@JsonKey(name: 'web')
final WebConfig? webConfig;
Expand Down
4 changes: 4 additions & 0 deletions lib/config/config.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 40 additions & 0 deletions lib/ios.dart
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ void createIcons(Config config, String? flavor) {
return;
}
if (config.removeAlphaIOS && image.hasAlpha) {
final backgroundColor = _getBackgroundColor(config);
final pixel = image.getPixel(0, 0);
do {
pixel.set(_alphaBlend(pixel, backgroundColor));
} while (pixel.moveNext());

image = image.convert(numChannels: 3);
}
if (image.hasAlpha) {
Expand Down Expand Up @@ -388,3 +394,37 @@ List<Map<String, String>> createImageList(String fileNamePrefix) {
];
return imageList;
}

ColorUint8 _getBackgroundColor(Config config) {
final backgroundColorHex = config.backgroundColorIOS.startsWith('#')
? config.backgroundColorIOS.substring(1)
: config.backgroundColorIOS;
if (backgroundColorHex.length != 6) {
throw Exception('background_color_ios hex should be 6 characters long');
}

final backgroundByte = int.parse(backgroundColorHex, radix: 16);
return ColorUint8.rgba(
(backgroundByte >> 16) & 0xff,
(backgroundByte >> 8) & 0xff,
(backgroundByte >> 0) & 0xff,
0xff,
);
}

Color _alphaBlend(Color fg, ColorUint8 bg) {
if (fg.format != Format.uint8) {
fg = fg.convert(format: Format.uint8);
}
if (fg.a == 0) {
return bg;
} else {
final invAlpha = 0xff - fg.a;
return ColorUint8.rgba(
(fg.a * fg.r + invAlpha * bg.g) ~/ 0xff,
(fg.a * fg.g + invAlpha * bg.a) ~/ 0xff,
(fg.a * fg.b + invAlpha * bg.b) ~/ 0xff,
0xff,
);
}
}
5 changes: 5 additions & 0 deletions test/abs/icon_generator_test.mocks.dart
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ class MockConfig extends _i1.Mock implements _i3.Config {
returnValue: false,
) as bool);
@override
String get backgroundColorIOS => (super.noSuchMethod(
Invocation.getter(#backgroundColorIOS),
returnValue: '',
) as String);
@override
bool get hasAndroidAdaptiveConfig => (super.noSuchMethod(
Invocation.getter(#hasAndroidAdaptiveConfig),
returnValue: false,
Expand Down
6 changes: 6 additions & 0 deletions test/macos/macos_icon_generator_test.mocks.dart
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ class MockConfig extends _i1.Mock implements _i3.Config {
returnValueForMissingStub: false,
) as bool);
@override
String get backgroundColorIOS => (super.noSuchMethod(
Invocation.getter(#backgroundColorIOS),
returnValue: '',
returnValueForMissingStub: '',
) as String);
@override
bool get hasAndroidAdaptiveConfig => (super.noSuchMethod(
Invocation.getter(#hasAndroidAdaptiveConfig),
returnValue: false,
Expand Down
5 changes: 5 additions & 0 deletions test/windows/windows_icon_generator_test.mocks.dart
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ class MockConfig extends _i1.Mock implements _i3.Config {
returnValue: false,
) as bool);
@override
String get backgroundColorIOS => (super.noSuchMethod(
Invocation.getter(#backgroundColorIOS),
returnValue: '',
) as String);
@override
bool get hasAndroidAdaptiveConfig => (super.noSuchMethod(
Invocation.getter(#hasAndroidAdaptiveConfig),
returnValue: false,
Expand Down

0 comments on commit 88ab71c

Please sign in to comment.