Skip to content
Permalink
Browse files

[schemes] Add dynamic color scheme MDCColorSchemeDefaultsMaterial2019…

…07. (#7848)
  • Loading branch information
wenyuzhang666 committed Jul 10, 2019
1 parent 7c7208f commit 6f83e12273d3739dba06f48a1cac3d09865b3cb2
@@ -88,6 +88,10 @@ typedef NS_ENUM(NSInteger, MDCColorSchemeDefaults) {
The Material Dark Mode defaults, circa July 2019.
*/
MDCColorSchemeDefaultsMaterialDark201907,
/**
The Material defaults supporting dynamic color for iOS 13, circa July 2019.
*/
MDCColorSchemeDefaultsMaterial201907,
};

/**
@@ -14,6 +14,25 @@

#import "MDCSemanticColorScheme.h"

static UIColor *DynamicColor(UIColor *defaultColor, UIColor *darkColor) {
#if defined(__IPHONE_13_0) && (__IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_13_0)
if (@available(iOS 13.0, *)) {
return [UIColor
colorWithDynamicProvider:^UIColor *_Nonnull(UITraitCollection *_Nonnull traitCollection) {
if (traitCollection.userInterfaceStyle == UIUserInterfaceStyleDark) {
return darkColor;
} else {
return defaultColor;
}
}];
} else {
return defaultColor;
}
#else
return defaultColor;
#endif
}

static UIColor *ColorFromRGB(uint32_t colorValue) {
return [UIColor colorWithRed:(CGFloat)(((colorValue >> 16) & 0xFF) / 255.0)
green:(CGFloat)(((colorValue >> 8) & 0xFF) / 255.0)
@@ -69,6 +88,18 @@ - (instancetype)initWithDefaults:(MDCColorSchemeDefaults)defaults {
_onSurfaceColor = ColorFromRGB(0xFFFFFF);
_onBackgroundColor = ColorFromRGB(0xFFFFFF);
break;
case MDCColorSchemeDefaultsMaterial201907:
_primaryColor = DynamicColor(ColorFromRGB(0x6200EE), ColorFromRGB(0xBB86FC));
_primaryColorVariant = DynamicColor(ColorFromRGB(0x3700B3), ColorFromRGB(0x3700B3));
_secondaryColor = DynamicColor(ColorFromRGB(0x03DAC6), ColorFromRGB(0x03DAC6));
_errorColor = DynamicColor(ColorFromRGB(0xB00020), ColorFromRGB(0xCF6679));
_surfaceColor = DynamicColor(ColorFromRGB(0xFFFFFF), ColorFromRGB(0x121212));
_backgroundColor = DynamicColor(ColorFromRGB(0xFFFFFF), ColorFromRGB(0x121212));
_onPrimaryColor = DynamicColor(ColorFromRGB(0xFFFFFF), ColorFromRGB(0x000000));
_onSecondaryColor = DynamicColor(ColorFromRGB(0x000000), ColorFromRGB(0x000000));
_onSurfaceColor = DynamicColor(ColorFromRGB(0x000000), ColorFromRGB(0xFFFFFF));
_onBackgroundColor = DynamicColor(ColorFromRGB(0x000000), ColorFromRGB(0xFFFFFF));
break;
}
}
return self;
@@ -85,6 +85,117 @@ - (void)testInitWithMaterialDefaultsDark {
XCTAssertEqualObjects(colorScheme.onBackgroundColor, ColorFromRGB(0xFFFFFF));
}

- (void)testInitWithMaterialDefaults201907WhenUserInterfaceStyleIsDarkForiOS13 {
#if defined(__IPHONE_13_0) && (__IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_13_0)
if (@available(iOS 13.0, *)) {
// Given
MDCSemanticColorScheme *colorScheme =
[[MDCSemanticColorScheme alloc] initWithDefaults:MDCColorSchemeDefaultsMaterial201907];

// When
UITraitCollection *traitCollection =
[UITraitCollection traitCollectionWithUserInterfaceStyle:UIUserInterfaceStyleDark];

// Then
XCTAssertEqualObjects(
[colorScheme.primaryColor resolvedColorWithTraitCollection:traitCollection],
ColorFromRGB(0xBB86FC));
XCTAssertEqualObjects(
[colorScheme.primaryColorVariant resolvedColorWithTraitCollection:traitCollection],
ColorFromRGB(0x3700B3));
XCTAssertEqualObjects(
[colorScheme.secondaryColor resolvedColorWithTraitCollection:traitCollection],
ColorFromRGB(0x03DAC6));
XCTAssertEqualObjects([colorScheme.errorColor resolvedColorWithTraitCollection:traitCollection],
ColorFromRGB(0xCF6679));
XCTAssertEqualObjects(
[colorScheme.surfaceColor resolvedColorWithTraitCollection:traitCollection],
ColorFromRGB(0x121212));
XCTAssertEqualObjects(
[colorScheme.backgroundColor resolvedColorWithTraitCollection:traitCollection],
ColorFromRGB(0x121212));
XCTAssertEqualObjects(
[colorScheme.onPrimaryColor resolvedColorWithTraitCollection:traitCollection],
ColorFromRGB(0x000000));
XCTAssertEqualObjects(
[colorScheme.onSecondaryColor resolvedColorWithTraitCollection:traitCollection],
ColorFromRGB(0x000000));
XCTAssertEqualObjects(
[colorScheme.onSurfaceColor resolvedColorWithTraitCollection:traitCollection],
ColorFromRGB(0xFFFFFF));
XCTAssertEqualObjects(
[colorScheme.onBackgroundColor resolvedColorWithTraitCollection:traitCollection],
ColorFromRGB(0xFFFFFF));
}
#endif
}

- (void)testInitWithMaterialDefaults201907WhenUserInterfaceStyleIsLightForiOS13 {
#if defined(__IPHONE_13_0) && (__IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_13_0)
if (@available(iOS 13.0, *)) {
// Given
MDCSemanticColorScheme *colorScheme =
[[MDCSemanticColorScheme alloc] initWithDefaults:MDCColorSchemeDefaultsMaterial201907];

// When
UITraitCollection *traitCollection =
[UITraitCollection traitCollectionWithUserInterfaceStyle:UIUserInterfaceStyleLight];

// Then
XCTAssertEqualObjects(
[colorScheme.primaryColor resolvedColorWithTraitCollection:traitCollection],
ColorFromRGB(0x6200EE));
XCTAssertEqualObjects(
[colorScheme.primaryColorVariant resolvedColorWithTraitCollection:traitCollection],
ColorFromRGB(0x3700B3));
XCTAssertEqualObjects(
[colorScheme.secondaryColor resolvedColorWithTraitCollection:traitCollection],
ColorFromRGB(0x03DAC6));
XCTAssertEqualObjects([colorScheme.errorColor resolvedColorWithTraitCollection:traitCollection],
ColorFromRGB(0xB00020));
XCTAssertEqualObjects(
[colorScheme.surfaceColor resolvedColorWithTraitCollection:traitCollection],
ColorFromRGB(0xFFFFFF));
XCTAssertEqualObjects(
[colorScheme.backgroundColor resolvedColorWithTraitCollection:traitCollection],
ColorFromRGB(0xFFFFFF));
XCTAssertEqualObjects(
[colorScheme.onPrimaryColor resolvedColorWithTraitCollection:traitCollection],
ColorFromRGB(0xFFFFFF));
XCTAssertEqualObjects(
[colorScheme.onSecondaryColor resolvedColorWithTraitCollection:traitCollection],
ColorFromRGB(0x000000));
XCTAssertEqualObjects(
[colorScheme.onSurfaceColor resolvedColorWithTraitCollection:traitCollection],
ColorFromRGB(0x000000));
XCTAssertEqualObjects(
[colorScheme.onBackgroundColor resolvedColorWithTraitCollection:traitCollection],
ColorFromRGB(0x000000));
}
#endif
}

- (void)testInitWithMaterialDefaults201907WhenUserInterfaceStyleIsLightForPreiOS13 {
if (@available(iOS 13.0, *)) {
} else {
// Given
MDCSemanticColorScheme *colorScheme =
[[MDCSemanticColorScheme alloc] initWithDefaults:MDCColorSchemeDefaultsMaterial201907];

// Then
XCTAssertEqualObjects(colorScheme.primaryColor, ColorFromRGB(0x6200EE));
XCTAssertEqualObjects(colorScheme.primaryColorVariant, ColorFromRGB(0x3700B3));
XCTAssertEqualObjects(colorScheme.secondaryColor, ColorFromRGB(0x03DAC6));
XCTAssertEqualObjects(colorScheme.errorColor, ColorFromRGB(0xB00020));
XCTAssertEqualObjects(colorScheme.surfaceColor, ColorFromRGB(0xFFFFFF));
XCTAssertEqualObjects(colorScheme.backgroundColor, ColorFromRGB(0xFFFFFF));
XCTAssertEqualObjects(colorScheme.onPrimaryColor, ColorFromRGB(0xFFFFFF));
XCTAssertEqualObjects(colorScheme.onSecondaryColor, ColorFromRGB(0x000000));
XCTAssertEqualObjects(colorScheme.onSurfaceColor, ColorFromRGB(0x000000));
XCTAssertEqualObjects(colorScheme.onBackgroundColor, ColorFromRGB(0x000000));
}
}

- (void)testColorMergeForOpaqueColor {
UIColor *backgroundColor = [UIColor whiteColor];
UIColor *blendColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:1.0];

0 comments on commit 6f83e12

Please sign in to comment.
You can’t perform that action at this time.