fix(ios): Color.fromIosColor returns wrong value (#10059)

This commit is contained in:
Mohamed Akram
2022-11-08 05:51:50 +04:00
committed by GitHub
parent 0f7e4ed0f3
commit b7d340f69b
5 changed files with 29 additions and 3 deletions

View File

@ -0,0 +1 @@
export * from './color-tests-common';

View File

@ -0,0 +1,2 @@
import * as android from './color-tests.android';
import * as iOS from './color-tests.ios';

View File

@ -0,0 +1,20 @@
// >> color-require
import * as colorModule from '@nativescript/core/color';
const { Color } = colorModule;
// << color-require
import * as TKUnit from '../tk-unit';
export * from './color-tests-common';
export function testFromIosColorWhite () {
// >> color-ios-white
// Creates the white color
const color = Color.fromIosColor(UIColor.whiteColor);
// << color-ios-white
TKUnit.assertEqual(color.a, 255, 'Color.a not properly parsed');
TKUnit.assertEqual(color.r, 255, 'Color.r not properly parsed');
TKUnit.assertEqual(color.g, 255, 'Color.g not properly parsed');
TKUnit.assertEqual(color.b, 255, 'Color.b not properly parsed');
TKUnit.assertEqual(color.hex, '#FFFFFF', 'Color.hex not properly parsed');
TKUnit.assertEqual(color.argb, 0xffffffff, 'Color.argb not properly parsed');
};

View File

@ -13,8 +13,11 @@ export class Color extends common.Color {
}
public static fromIosColor(value: UIColor): Color {
const rgba = CGColorGetComponents(value.CGColor);
return new Color(Math.round(rgba[3] * 255), Math.round(rgba[0] * 255), Math.round(rgba[1] * 255), Math.round(rgba[2] * 255));
const r = new interop.Reference<number>();
const g = new interop.Reference<number>();
const b = new interop.Reference<number>();
const a = new interop.Reference<number>();
value.getRedGreenBlueAlpha(r, g, b, a);
return new Color(Math.round(a.value * 255), Math.round(r.value * 255), Math.round(g.value * 255), Math.round(b.value * 255));
}
}