Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
M
Material
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Dmitriy Stepanets
Material
Commits
7cd9489f
Commit
7cd9489f
authored
Oct 09, 2018
by
Orkhan Alikhanov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added UIColor.blend(with:)
parent
f7ba4c9e
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
37 additions
and
0 deletions
+37
-0
Sources/iOS/Material+UIColor.swift
+37
-0
No files found.
Sources/iOS/Material+UIColor.swift
View file @
7cd9489f
...
@@ -62,3 +62,40 @@ public extension UIColor {
...
@@ -62,3 +62,40 @@ public extension UIColor {
self
.
init
(
argb
:
(
0xff000000
as
UInt32
)
|
rgb
)
self
.
init
(
argb
:
(
0xff000000
as
UInt32
)
|
rgb
)
}
}
}
}
internal
extension
UIColor
{
/// A tuple of the rgba components.
var
components
:
(
r
:
CGFloat
,
g
:
CGFloat
,
b
:
CGFloat
,
a
:
CGFloat
)
{
var
r
:
CGFloat
=
0
var
g
:
CGFloat
=
0
var
b
:
CGFloat
=
0
var
a
:
CGFloat
=
0
getRed
(
&
r
,
green
:
&
g
,
blue
:
&
b
,
alpha
:
&
a
)
return
(
r
,
g
,
b
,
a
)
}
/**
Blends given coverColor over this color.
- Parameter with coverColor: A UIColor.
- Returns: Resultant color of blending.
*/
func
blend
(
with
coverColor
:
UIColor
)
->
UIColor
{
/// Blends channels according to https://en.wikipedia.org/wiki/Alpha_compositing (see `over` operator).
func
blendChannel
(
value
:
CGFloat
,
bValue
:
CGFloat
,
alpha
:
CGFloat
,
bAlpha
:
CGFloat
)
->
CGFloat
{
return
((
1
-
alpha
)
*
bValue
*
bAlpha
+
alpha
*
value
)
/
(
alpha
+
bAlpha
*
(
1
-
alpha
))
}
let
(
r
,
g
,
b
,
a
)
=
coverColor
.
components
let
(
bR
,
bG
,
bB
,
bA
)
=
components
let
newR
=
blendChannel
(
value
:
r
,
bValue
:
bR
,
alpha
:
a
,
bAlpha
:
bA
)
let
newG
=
blendChannel
(
value
:
g
,
bValue
:
bG
,
alpha
:
a
,
bAlpha
:
bA
)
let
newB
=
blendChannel
(
value
:
b
,
bValue
:
bB
,
alpha
:
a
,
bAlpha
:
bA
)
let
newA
=
a
+
bA
*
(
1
-
a
)
return
UIColor
(
red
:
newR
,
green
:
newG
,
blue
:
newB
,
alpha
:
newA
)
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment