forked from appsquickly/typhoon
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Egor Tolstoy
committed
Nov 4, 2015
1 parent
16e13dc
commit 8fd803a
Showing
9 changed files
with
305 additions
and
55 deletions.
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
Source/TypeConversion/Helpers/TyphoonColorConversionUtils.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
//////////////////////////////////////////////////////////////////////////////// | ||
// | ||
// TYPHOON FRAMEWORK | ||
// Copyright 2015, Typhoon Framework Contributors | ||
// All Rights Reserved. | ||
// | ||
// NOTICE: The authors permit you to use, modify, and distribute this file | ||
// in accordance with the terms of the license agreement accompanying it. | ||
// | ||
//////////////////////////////////////////////////////////////////////////////// | ||
|
||
#import <Foundation/Foundation.h> | ||
|
||
struct RGBA | ||
{ | ||
CGFloat red; | ||
CGFloat green; | ||
CGFloat blue; | ||
CGFloat alpha; | ||
} rgba; | ||
|
||
@interface TyphoonColorConversionUtils : NSObject | ||
|
||
+ (struct RGBA)colorFromHexString:(NSString *)hexString; | ||
+ (struct RGBA)colorFromCssStyleString:(NSString *)cssString; | ||
|
||
@end |
64 changes: 64 additions & 0 deletions
64
Source/TypeConversion/Helpers/TyphoonColorConversionUtils.m
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
//////////////////////////////////////////////////////////////////////////////// | ||
// | ||
// TYPHOON FRAMEWORK | ||
// Copyright 2015, Typhoon Framework Contributors | ||
// All Rights Reserved. | ||
// | ||
// NOTICE: The authors permit you to use, modify, and distribute this file | ||
// in accordance with the terms of the license agreement accompanying it. | ||
// | ||
//////////////////////////////////////////////////////////////////////////////// | ||
|
||
#import "TyphoonColorConversionUtils.h" | ||
|
||
@implementation TyphoonColorConversionUtils | ||
|
||
+ (struct RGBA)colorFromHexString:(NSString *)hexString { | ||
hexString = | ||
[[hexString stringByReplacingOccurrencesOfString:@"#" withString:@""] stringByReplacingOccurrencesOfString:@"0x" withString:@""]; | ||
|
||
unsigned int red, green, blue, alpha; | ||
if ([hexString length] == 6) { | ||
sscanf([hexString UTF8String], "%02X%02X%02X", &red, &green, &blue); | ||
alpha = 255; | ||
} | ||
else if ([hexString length] == 8) { | ||
sscanf([hexString UTF8String], "%02X%02X%02X%02X", &alpha, &red, &green, &blue); | ||
} | ||
else { | ||
[NSException raise:NSInvalidArgumentException format:@"%@ requires a six or eight digit hex string.", | ||
NSStringFromClass([self class])]; | ||
} | ||
return [self colorFromRed:red green:green blue:blue alpha:(CGFloat)(alpha / 255.0)]; | ||
} | ||
|
||
+ (struct RGBA)colorFromCssStyleString:(NSString *)cssString { | ||
NSArray *colorComponents = [cssString componentsSeparatedByString:@","]; | ||
|
||
unsigned int red, green, blue; | ||
float alpha; | ||
if ([colorComponents count] == 3) { | ||
sscanf([cssString UTF8String], "%d,%d,%d", &red, &green, &blue); | ||
alpha = 1.0; | ||
} | ||
else if ([colorComponents count] == 4) { | ||
sscanf([cssString UTF8String], "%d,%d,%d,%f", &red, &green, &blue, &alpha); | ||
} | ||
else { | ||
[NSException raise:NSInvalidArgumentException format:@"%@ requires css style format UIColor(r,g,b) or UIColor(r,g,b,a).", | ||
NSStringFromClass([self class])]; | ||
} | ||
return [self colorFromRed:red green:green blue:blue alpha:alpha]; | ||
} | ||
|
||
+ (struct RGBA)colorFromRed:(NSUInteger)red green:(NSUInteger)green blue:(NSUInteger)blue alpha:(CGFloat)alpha | ||
{ | ||
struct RGBA color; | ||
color.red = (CGFloat)red / 255.0; | ||
color.green = (CGFloat)green / 255.0; | ||
color.blue = (CGFloat)blue / 255.0; | ||
color.alpha = (CGFloat)alpha; | ||
return color; | ||
} | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
Source/osx/TypeConversion/Converters/TyphoonNSColorTypeConverter.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
//////////////////////////////////////////////////////////////////////////////// | ||
// | ||
// TYPHOON FRAMEWORK | ||
// Copyright 2015, Typhoon Framework Contributors | ||
// All Rights Reserved. | ||
// | ||
// NOTICE: The authors permit you to use, modify, and distribute this file | ||
// in accordance with the terms of the license agreement accompanying it. | ||
// | ||
//////////////////////////////////////////////////////////////////////////////// | ||
|
||
#import "TyphoonTypeConverter.h" | ||
|
||
/** | ||
* A type converter for NSColor. | ||
* | ||
* The formats supported are: | ||
* Hexadecimal, #RRGGBB or #AARRGGBB | ||
* Css-style, rgb(r,g,b) or rgba(r,g,b,a) | ||
* | ||
*/ | ||
@interface TyphoonNSColorTypeConverter : NSObject <TyphoonTypeConverter> | ||
|
||
@end |
44 changes: 44 additions & 0 deletions
44
Source/osx/TypeConversion/Converters/TyphoonNSColorTypeConverter.m
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
//////////////////////////////////////////////////////////////////////////////// | ||
// | ||
// TYPHOON FRAMEWORK | ||
// Copyright 2015, Typhoon Framework Contributors | ||
// All Rights Reserved. | ||
// | ||
// NOTICE: The authors permit you to use, modify, and distribute this file | ||
// in accordance with the terms of the license agreement accompanying it. | ||
// | ||
//////////////////////////////////////////////////////////////////////////////// | ||
|
||
#import "TyphoonNSColorTypeConverter.h" | ||
|
||
#import "TyphoonColorConversionUtils.h" | ||
|
||
@implementation TyphoonNSColorTypeConverter | ||
|
||
- (id)supportedType | ||
{ | ||
return @"NSColor"; | ||
} | ||
|
||
- (id)convert:(NSString *)stringValue | ||
{ | ||
stringValue = [TyphoonTypeConversionUtils textWithoutTypeFromTextValue:stringValue]; | ||
|
||
struct RGBA color; | ||
|
||
if ([stringValue hasPrefix:@"#"] || [stringValue hasPrefix:@"0x"]) { | ||
color = [TyphoonColorConversionUtils colorFromHexString:stringValue]; | ||
} | ||
else { | ||
color = [TyphoonColorConversionUtils colorFromCssStyleString:stringValue]; | ||
} | ||
|
||
return [self colorFromRGBA:color]; | ||
} | ||
|
||
- (NSColor *)colorFromRGBA:(struct RGBA)rgba | ||
{ | ||
return [NSColor colorWithRed:rgba.red green:rgba.green blue:rgba.blue alpha:rgba.alpha]; | ||
} | ||
|
||
@end |
76 changes: 76 additions & 0 deletions
76
Tests/OSX/TypeConversion/TyphoonNSColorTypeConverterTests.m
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
//////////////////////////////////////////////////////////////////////////////// | ||
// | ||
// TYPHOON FRAMEWORK | ||
// Copyright 2015, Typhoon Framework Contributors | ||
// All Rights Reserved. | ||
// | ||
// NOTICE: The authors permit you to use, modify, and distribute this file | ||
// in accordance with the terms of the license agreement accompanying it. | ||
// | ||
//////////////////////////////////////////////////////////////////////////////// | ||
|
||
#import <XCTest/XCTest.h> | ||
|
||
#import "TyphoonTypeConverterRegistry.h" | ||
#import "TyphoonTypeConverter.h" | ||
|
||
@interface TyphoonNSColorTypeConverterTests : XCTestCase | ||
|
||
@property(nonatomic, strong, readonly) NSColor *color; | ||
@property(nonatomic, strong) id <TyphoonTypeConverter> converter; | ||
|
||
@end | ||
|
||
@implementation TyphoonNSColorTypeConverterTests | ||
|
||
- (void)setUp { | ||
[super setUp]; | ||
|
||
TyphoonTypeConverterRegistry *registry = [[TyphoonTypeConverterRegistry alloc] init]; | ||
self.converter = [registry converterForType:@"NSColor"]; | ||
} | ||
|
||
- (void)tearDown { | ||
self.converter = nil; | ||
|
||
[super tearDown]; | ||
} | ||
|
||
- (void)assertColor:(NSColor *)color red:(CGFloat)red green:(CGFloat)green blue:(CGFloat)blue alpha:(CGFloat)alpha | ||
{ | ||
XCTAssertNotNil(color); | ||
|
||
CGFloat redComponent, greenComponent, blueComponent, alphaComponent; | ||
[color getRed:&redComponent green:&greenComponent blue:&blueComponent alpha:&alphaComponent]; | ||
|
||
XCTAssertEqual(redComponent, red); | ||
XCTAssertEqual(greenComponent, green); | ||
XCTAssertEqual(blueComponent, blue); | ||
XCTAssertEqual(alphaComponent, alpha); | ||
} | ||
|
||
- (void)test_converts_hex_string | ||
{ | ||
NSColor *color = [self.converter convert:@"NSColor(#ffffff)"]; | ||
[self assertColor:color red:1.0f green:1.0f blue:1.0f alpha:1.0f]; | ||
} | ||
|
||
- (void)test_converts_hex_string_with_alpha | ||
{ | ||
NSColor *color = [self.converter convert:@"NSColor(#00ffffff)"]; | ||
[self assertColor:color red:1.0f green:1.0f blue:1.0f alpha:0.0f]; | ||
} | ||
|
||
- (void)test_converts_css_style_rgb | ||
{ | ||
NSColor *color = [self.converter convert:@"NSColor(255,255,255)"]; | ||
[self assertColor:color red:1.0f green:1.0f blue:1.0f alpha:1.0f]; | ||
} | ||
|
||
- (void)test_converts_css_style_rgba | ||
{ | ||
NSColor *color = [self.converter convert:@"NSColor(255,255,255,0.5)"]; | ||
[self assertColor:color red:1.0f green:1.0f blue:1.0f alpha:0.5f]; | ||
} | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.