Skip to content

Commit dbf3712

Browse files
committed
add color mixing feature
1 parent 77af4b9 commit dbf3712

File tree

4 files changed

+57
-0
lines changed

4 files changed

+57
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//
2+
// UIColor+YAColorMixing.h
3+
//
4+
//
5+
// Created by liuyan on 7/8/15.
6+
//
7+
//
8+
9+
#import <UIKit/UIKit.h>
10+
11+
@interface UIColor (YAColorMixing)
12+
13+
- (UIColor *)colorByAddingColor:(UIColor *)addingColor;
14+
15+
@end
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//
2+
// UIColor+YAColorMixing.m
3+
//
4+
//
5+
// Created by liuyan on 7/8/15.
6+
//
7+
//
8+
9+
#import "UIColor+YAColorMixing.h"
10+
11+
@implementation UIColor (YAColorMixing)
12+
13+
- (UIColor *)colorByAddingColor:(UIColor *)addingColor
14+
{
15+
CGFloat upperRed, upperGreen, upperBlue, upperAlpha = 0;
16+
CGFloat underRed, underGreen, underBlue, underAlpha = 0;
17+
CGFloat resultRed, resultGreen, resultBlue, resultAlpha = 0;
18+
19+
[addingColor getRed:&upperRed green:&upperGreen blue:&upperBlue alpha:&upperAlpha];
20+
[self getRed:&underRed green:&underGreen blue:&underBlue alpha:&underAlpha];
21+
22+
resultRed = upperRed * upperAlpha + underRed * underAlpha * (1 - upperAlpha);
23+
resultGreen = upperGreen * upperAlpha + underGreen * underAlpha * (1 - upperAlpha);
24+
resultBlue = upperBlue * upperAlpha + underBlue * underAlpha * (1 - upperAlpha);
25+
resultAlpha = 1 - (1 - upperAlpha) * (1 - underAlpha);
26+
27+
resultRed /= resultAlpha;
28+
resultGreen /= resultAlpha;
29+
resultBlue /= resultAlpha;
30+
31+
return [UIColor colorWithRed:resultRed green:resultGreen blue:resultBlue alpha:resultAlpha];
32+
}
33+
34+
@end

Source/YAUIKit.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323

2424
#import "UIFont+YAUIKit.h"
2525

26+
#import "UIColor+YAColorMixing.h"
27+
2628
#import "UIImage+YAColor.h"
2729
#import "UIImage+YAImageTransform.h"
2830
#import "UIImage+YAUIKit.h"

YAUIKit/YAUIKit.xcodeproj/project.pbxproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@
8888
75900D4819E445E500C05D28 /* YASkinManager.m in Sources */ = {isa = PBXBuildFile; fileRef = 75900D4719E445E500C05D28 /* YASkinManager.m */; };
8989
75900D4C19E448F100C05D28 /* YATableDataSource.m in Sources */ = {isa = PBXBuildFile; fileRef = 75900D4B19E448F100C05D28 /* YATableDataSource.m */; };
9090
75C15C2419EA70AA00E5C341 /* UIApplication+YAKeyboardWindow.m in Sources */ = {isa = PBXBuildFile; fileRef = 75C15C2319EA70AA00E5C341 /* UIApplication+YAKeyboardWindow.m */; };
91+
75D8F39F1B4D023300EBC7E9 /* UIColor+YAColorMixing.m in Sources */ = {isa = PBXBuildFile; fileRef = 75D8F39E1B4D023300EBC7E9 /* UIColor+YAColorMixing.m */; };
9192
75F9D1321B2A8DF800972CB5 /* UITableView+YAReusableCell.m in Sources */ = {isa = PBXBuildFile; fileRef = 75F9D1311B2A8DF800972CB5 /* UITableView+YAReusableCell.m */; };
9293
/* End PBXBuildFile section */
9394

@@ -215,6 +216,8 @@
215216
75900D4B19E448F100C05D28 /* YATableDataSource.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = YATableDataSource.m; sourceTree = "<group>"; };
216217
75C15C2219EA70AA00E5C341 /* UIApplication+YAKeyboardWindow.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UIApplication+YAKeyboardWindow.h"; sourceTree = "<group>"; };
217218
75C15C2319EA70AA00E5C341 /* UIApplication+YAKeyboardWindow.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UIApplication+YAKeyboardWindow.m"; sourceTree = "<group>"; };
219+
75D8F39D1B4D023300EBC7E9 /* UIColor+YAColorMixing.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UIColor+YAColorMixing.h"; sourceTree = "<group>"; };
220+
75D8F39E1B4D023300EBC7E9 /* UIColor+YAColorMixing.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UIColor+YAColorMixing.m"; sourceTree = "<group>"; };
218221
75F9D1301B2A8DF800972CB5 /* UITableView+YAReusableCell.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UITableView+YAReusableCell.h"; sourceTree = "<group>"; };
219222
75F9D1311B2A8DF800972CB5 /* UITableView+YAReusableCell.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UITableView+YAReusableCell.m"; sourceTree = "<group>"; };
220223
/* End PBXFileReference section */
@@ -503,6 +506,8 @@
503506
75900CF219E4438E00C05D28 /* UIColor */ = {
504507
isa = PBXGroup;
505508
children = (
509+
75D8F39D1B4D023300EBC7E9 /* UIColor+YAColorMixing.h */,
510+
75D8F39E1B4D023300EBC7E9 /* UIColor+YAColorMixing.m */,
506511
75900CF319E4438E00C05D28 /* UIColor+YAHexColor.h */,
507512
75900CF419E4438E00C05D28 /* UIColor+YAHexColor.m */,
508513
);
@@ -798,6 +803,7 @@
798803
buildActionMask = 2147483647;
799804
files = (
800805
75900D3819E4438E00C05D28 /* YASeparatorLine.m in Sources */,
806+
75D8F39F1B4D023300EBC7E9 /* UIColor+YAColorMixing.m in Sources */,
801807
75900D2F19E4438E00C05D28 /* UIBarButtonItem+YAItemFactory.m in Sources */,
802808
7543F9D91A2342C0008178C8 /* YATextEditorViewController.m in Sources */,
803809
75900D3119E4438E00C05D28 /* UIControl+YAUIKit.m in Sources */,

0 commit comments

Comments
 (0)