Skip to content

Commit

Permalink
Updated for MaterialComponents for iOS version 34.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
ilap committed Sep 6, 2017
1 parent 583f4d5 commit f188520
Show file tree
Hide file tree
Showing 22 changed files with 693 additions and 48 deletions.
31 changes: 31 additions & 0 deletions MaterialComponentsXamarinExamples.sln
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MaterialComponents", "src\M
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ShrineRemoteImage", "samples\ShrineRemoteImage\ShrineRemoteImage.csproj", "{F06E20A4-BA44-4986-A8B3-E8529F025E5A}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ShrineXamarin", "samples\Shrine\ShrineXamarin.csproj", "{05DED03B-94AD-46B9-9BC9-57FB73DE3E79}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MDC-Tutorial", "samples\MDC-Tutorial\MDC-Tutorial.csproj", "{3AC64AE1-3149-4F53-A7A6-4CD760689AF3}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -35,5 +39,32 @@ Global
{F06E20A4-BA44-4986-A8B3-E8529F025E5A}.Release|iPhoneSimulator.Build.0 = Release|iPhoneSimulator
{F06E20A4-BA44-4986-A8B3-E8529F025E5A}.Debug|iPhone.ActiveCfg = Debug|iPhone
{F06E20A4-BA44-4986-A8B3-E8529F025E5A}.Debug|iPhone.Build.0 = Debug|iPhone
{05DED03B-94AD-46B9-9BC9-57FB73DE3E79}.Debug|Any CPU.ActiveCfg = Debug|iPhoneSimulator
{05DED03B-94AD-46B9-9BC9-57FB73DE3E79}.Debug|Any CPU.Build.0 = Debug|iPhoneSimulator
{05DED03B-94AD-46B9-9BC9-57FB73DE3E79}.Release|Any CPU.ActiveCfg = Release|iPhone
{05DED03B-94AD-46B9-9BC9-57FB73DE3E79}.Release|Any CPU.Build.0 = Release|iPhone
{05DED03B-94AD-46B9-9BC9-57FB73DE3E79}.Debug|iPhoneSimulator.ActiveCfg = Debug|iPhoneSimulator
{05DED03B-94AD-46B9-9BC9-57FB73DE3E79}.Debug|iPhoneSimulator.Build.0 = Debug|iPhoneSimulator
{05DED03B-94AD-46B9-9BC9-57FB73DE3E79}.Release|iPhone.ActiveCfg = Release|iPhone
{05DED03B-94AD-46B9-9BC9-57FB73DE3E79}.Release|iPhone.Build.0 = Release|iPhone
{05DED03B-94AD-46B9-9BC9-57FB73DE3E79}.Release|iPhoneSimulator.ActiveCfg = Release|iPhoneSimulator
{05DED03B-94AD-46B9-9BC9-57FB73DE3E79}.Release|iPhoneSimulator.Build.0 = Release|iPhoneSimulator
{05DED03B-94AD-46B9-9BC9-57FB73DE3E79}.Debug|iPhone.ActiveCfg = Debug|iPhone
{05DED03B-94AD-46B9-9BC9-57FB73DE3E79}.Debug|iPhone.Build.0 = Debug|iPhone
{3AC64AE1-3149-4F53-A7A6-4CD760689AF3}.Debug|Any CPU.ActiveCfg = Debug|iPhoneSimulator
{3AC64AE1-3149-4F53-A7A6-4CD760689AF3}.Debug|Any CPU.Build.0 = Debug|iPhoneSimulator
{3AC64AE1-3149-4F53-A7A6-4CD760689AF3}.Release|Any CPU.ActiveCfg = Release|iPhone
{3AC64AE1-3149-4F53-A7A6-4CD760689AF3}.Release|Any CPU.Build.0 = Release|iPhone
{3AC64AE1-3149-4F53-A7A6-4CD760689AF3}.Debug|iPhoneSimulator.ActiveCfg = Debug|iPhoneSimulator
{3AC64AE1-3149-4F53-A7A6-4CD760689AF3}.Debug|iPhoneSimulator.Build.0 = Debug|iPhoneSimulator
{3AC64AE1-3149-4F53-A7A6-4CD760689AF3}.Release|iPhone.ActiveCfg = Release|iPhone
{3AC64AE1-3149-4F53-A7A6-4CD760689AF3}.Release|iPhone.Build.0 = Release|iPhone
{3AC64AE1-3149-4F53-A7A6-4CD760689AF3}.Release|iPhoneSimulator.ActiveCfg = Release|iPhoneSimulator
{3AC64AE1-3149-4F53-A7A6-4CD760689AF3}.Release|iPhoneSimulator.Build.0 = Release|iPhoneSimulator
{3AC64AE1-3149-4F53-A7A6-4CD760689AF3}.Debug|iPhone.ActiveCfg = Debug|iPhone
{3AC64AE1-3149-4F53-A7A6-4CD760689AF3}.Debug|iPhone.Build.0 = Debug|iPhone
EndGlobalSection
GlobalSection(MonoDevelopProperties) = preSolution
version = 34.0.1
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#ifdef __OBJC__
#import <UIKit/UIKit.h>
#else
#ifndef FOUNDATION_EXPORT
#if defined(__cplusplus)
#define FOUNDATION_EXPORT extern "C"
#else
#define FOUNDATION_EXPORT extern
#endif
#endif
#endif

#import "MDFTextAccessibility.h"

FOUNDATION_EXPORT double MDFTextAccessibilityVersionNumber;
FOUNDATION_EXPORT const unsigned char MDFTextAccessibilityVersionString[];

Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
/*
Copyright 2016-present Google Inc. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

#import <UIKit/UIKit.h>

/** Options for selecting text colors that provide acceptable contrast ratios. */
typedef NS_OPTIONS(NSUInteger, MDFTextAccessibilityOptions) {
/** No options. */
MDFTextAccessibilityOptionsNone = 0,

/** Font size is at least 14pt bold or 18pt normal. */
MDFTextAccessibilityOptionsLargeFont = 1 << 0,

/** Do not modify alpha values to find good colors. */
MDFTextAccessibilityOptionsPreserveAlpha = 1 << 1,

/** Prefer darker colors to lighter colors. */
MDFTextAccessibilityOptionsPreferDarker = 1 << 2,

/** Prefer lighter colors to darker colors. */
MDFTextAccessibilityOptionsPreferLighter = 1 << 3,

/** Use enhanced contrast ratios (level AAA) instead of minimum ratios (level AA). */
MDFTextAccessibilityOptionsEnhancedContrast = 1 << 4,
};

/**
MDFTextAccessiblity provides methods to compute accessible text colors.
*/
@interface MDFTextAccessibility : NSObject

/**
An optionally transparent text color suitable for displaying on a background color with a
particular font.
The color returned will be white or black with an alpha value of targetTextAlpha, unless the
contrast ratio is insufficient, in which case the alpha is increased (made more opaque).
If the passed font is nil, then a conservative guess is used.
@param backgroundColor The opaque background color the text will be displayed on.
@param targetTextAlpha The target alpha of the text.
@param font The font the text will use or nil.
@return A color with acceptable contrast ratio for displaying text on |color|.
*/
+ (nonnull UIColor *)textColorOnBackgroundColor:(nonnull UIColor *)backgroundColor
targetTextAlpha:(CGFloat)targetTextAlpha
font:(nullable UIFont *)font;

/**
An optionally transparent text color suitable for displaying on a background image with a
particular font.
The color returned will be white or black with an alpha value of targetTextAlpha, unless the
contrast ratio is insufficient, in which case the alpha is increased (made more opaque).
If the passed font is nil, then a conservative guess is used.
The content of the background image is simply averaged to make an average color, which is then used
as if it were the background color of the text. Depending on the contents of the image, this
approximation may or may not result in legible text.
The supplied image region will be intersected with the image's bounds. If the resulting region is
null or empty then this method returns nil.
@param backgroundImage The opaque background image the text will be displayed on.
@param region The region in which the text will be displayed. Can be conservatively large.
@param targetTextAlpha The target alpha of the text.
@param font The font to be used to display the text. Can be nil.
@return A color with acceptable contrast ratio, or nil if the region is out of bounds of the image.
*/
+ (nullable UIColor *)textColorOnBackgroundImage:(nonnull UIImage *)backgroundImage
inRegion:(CGRect)region
targetTextAlpha:(CGFloat)targetTextAlpha
font:(nullable UIFont *)font;

#pragma mark Advanced methods

/**
An optionally transparent text color suitable for displaying text on a given opaque background
color.
This method calls textColorFromChoices:onColor:options: with the choices [white, black], both
with their alpha set to targetTextAlpha.
If MDFTextAccessibilityOptionsPreserveAlpha is included in the options, then the algorithm will not
modify the alpha values, which may result in no color being returned at all.
@param backgroundColor The opaque background color the text will be displayed on.
@param targetTextAlpha The target alpha of the text.
@param options A combination of MDFTextAccessibilityOptions values.
@return A color with acceptable contrast ratio or nil if no such color exists.
*/
+ (nullable UIColor *)textColorOnBackgroundColor:(nonnull UIColor *)backgroundColor
targetTextAlpha:(CGFloat)targetTextAlpha
options:(MDFTextAccessibilityOptions)options;

/**
A color selected from a set which is suitable for displaying text on a given opaque background
color.
Since the minimum ratio for "large" text is less stringent, set
the MDFTextAccessibilityOptionsLargeFont bit if the font size will be greater than or
equal to 18pt normal or 14pt bold. If in doubt or if the text size can vary, be conservative and do
not specify MDFTextAccessibilityOptionsLargeFont in the options.
By default, the first acceptable color in |choices| will be returned. If
MDFTextAccessibilityOptionsPreferLighter is set, then the lightest acceptable color will be
returned, and if MDFTextAccessibilityOptionsPreferDarker is set, then the darkest acceptable color
will be returned. This allows for a standard set of text colors to be used in different situations.
By default, the algorithm will attempt to modify the alpha value of colors in |choices| instead
of switching to an alternate color, under the assumption that text with slightly different
alpha values is less noticible than, for example, black text where white text is usually used.
If MDFTextAccessibilityOptionsPreserveAlpha is included in the options, then the algorithm will not
modify the alpha values, which may result in no color being returned at all.
@param choices An array of text color UIColor objects with optional alpha values.
@param backgroundColor The opaque background color the text will be displayed on.
@param options A combination of MDFTextAccessibilityOptions values.
@return A color with acceptable contrast ratio or nil if no such color exists.
*/
+ (nullable UIColor *)textColorFromChoices:(nonnull NSArray<UIColor *> *)choices
onBackgroundColor:(nonnull UIColor *)backgroundColor
options:(MDFTextAccessibilityOptions)options;

/**
The minimum alpha that text can have and still have an acceptable contrast ratio. Depending on
color combinations, the minimum useable alpha can vary.
Since the minimum ratio for "large" text is less stringent, set
the MDFTextAccessibilityOptionsLargeFont bit if the font size will be greater than or
equal to 18pt normal or 14pt bold. If in doubt or if the text size can vary, be conservative and do
not specify MDFTextAccessibilityOptionsLargeFont in the options.
@note There are some color combinations (white on white) for which an acceptable alpha value
doesn't exist.
@param textColor The text color (alpha is ignored).
@param backgroundColor The opaque background color the text will be displayed on.
@param options A combination of MDFTextAccessibilityOptions values.
@return The minimum alpha value to use in this situation, or -1 if there is no such alpha.
*/
+ (CGFloat)minAlphaOfTextColor:(nonnull UIColor *)textColor
onBackgroundColor:(nonnull UIColor *)backgroundColor
options:(MDFTextAccessibilityOptions)options;

/**
The contrast ratio of a text color when displayed on an opaque background color.
@param textColor A text color with optional transparency.
@param backgroundColor The opaque background color the text will be displayed on.
@return The contrast ratio of the text color on the background color.
*/
+ (CGFloat)contrastRatioForTextColor:(nonnull UIColor *)textColor
onBackgroundColor:(nonnull UIColor *)backgroundColor;

/**
Whether a text color passes accessibility standards when displayed on an opaque background color.
MDFTextAccessibilityOptionsLargeFont and MDFTextAccessibilityOptionsEnhancedContrast are relevant
options for this method.
@param textColor A text color with optional transparency.
@param backgroundColor The opaque background color the text will be displayed on.
@param options A combination of MDFTextAccessibilityOptions values.
@return YES if the text color would meet accessibility standards.
*/
+ (BOOL)textColor:(nonnull UIColor *)textColor
passesOnBackgroundColor:(nonnull UIColor *)backgroundColor
options:(MDFTextAccessibilityOptions)options;

/**
Whether a particular font would be considered "large" for the purposes of calculating
contrast ratios.
Large fonts are defined as greater than 18pt normal or 14pt bold. If the passed font is nil, then
this method returns NO.
For more see: https://www.w3.org/TR/UNDERSTANDING-WCAG20/visual-audio-contrast-contrast.html
@param font The font to examine, or nil.
@return YES if the font is non-nil and is considered "large".
*/
+ (BOOL)isLargeForContrastRatios:(nullable UIFont *)font;

@end
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
framework module MDFTextAccessibility {
umbrella header "MDFTextAccessibility-umbrella.h"

export *
module * { export * }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
Copyright 2016-present Google Inc. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

#import <UIKit/UIKit.h>

#if defined(__cplusplus)
extern "C" {
#endif

/**
Copies the RGBA components of color, expanding greyscale colors out to RGBA if necessary.
@param color The color to extract the components from.
@param rgbaComponents A pointer to four CGFloat values.
*/
void MDFCopyRGBAComponents(CGColorRef color, CGFloat *rgbaComponents);

/**
Returns the contrast ratio of a foreground color blended on top of a background color.
@param foregroundColorComponents A pointer to four RGBA CGFloats of the foreground color.
@param backgroundColorComponents A pointer to four RGBA CGFloats of the background color.
@return The contrast ratio of the two colors after blending.
*/
CGFloat MDFContrastRatioOfRGBAComponents(const CGFloat *foregroundColorComponents,
const CGFloat *backgroundColorComponents);

/**
Calculates the relative luminance of a sRGB color from its components (ignores alpha).
@see http://www.w3.org/TR/2008/REC-WCAG20-20081211/#relativeluminancedef
@param components A pointer to three RGB CGFloat values of the color.
@return The relative luminance of the color.
*/
CGFloat MDFRelativeLuminanceOfRGBComponents(const CGFloat *components);

/**
Calculates the minimum alpha that a foreground color can have such that, when it's blended on top
of an opaque background color, the resulting contrast ratio is higher than a minimum contrast
ratio.
If there is no such acceptable contrast ratio, then -1 is returned. This is the case when even a
completely opaque foreground color can't produce a high enough contrast ratio. For example, light
grey on white will have a low contrast ratio for any alpha value assigned to the light grey.
@param color The foreground color (alpha ignored).
@param backgroundColor The background color (assumed to be opaque).
@param minContrastRatio The minimum allowable contrast ratio.
@return The minimum acceptable alpha of the foreground color, or -1 if no such alpha exists.
*/
CGFloat MDFMinAlphaOfColorOnBackgroundColor(UIColor *color,
UIColor *backgroundColor,
CGFloat minContrastRatio);

#if defined __cplusplus
} // extern "C"
#endif
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
Copyright 2016-present Google Inc. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

#import <UIKit/UIKit.h>

#if defined(__cplusplus)
extern "C" {
#endif

/**
Return the average color of an image in a particular region.
The region will be intersected with the image's bounds. If the resulting region is empty (or the
input region was null) then this function returns nil.
@param image The image to examine.
@param region The region of the image to average, or CGRectInfinite for the entire image.
@return The average color, or nil if the region was invalid.
*/
UIColor *MDFAverageColorOfOpaqueImage(UIImage *image, CGRect region);

#if defined __cplusplus
} // extern "C"
#endif
Loading

0 comments on commit f188520

Please sign in to comment.