[ADD] Mix color method

This commit is contained in:
Yannick Loriot 2015-06-06 17:10:11 +02:00
parent 151e79202a
commit 9800488f6f
5 changed files with 86 additions and 18 deletions

View File

@ -7,7 +7,7 @@ Pod::Spec.new do |s|
s.social_media_url = 'https://twitter.com/yannickloriot'
s.authors = { 'Yannick Loriot' => 'contact@yannickloriot.com' }
s.source = { :git => 'https://github.com/yannickl/DynamicColor.git', :tag => s.version }
s.screenshot = 'http://yannickloriot.com/resources/dynamiccolor-example-screenshot.png'
s.screenshot = 'http://yannickloriot.com/resources/dynamiccolor-screenshot.png'
s.ios.deployment_target = '8.0'

View File

@ -137,7 +137,7 @@ public extension UIColor {
/**
Creates and returns a lighter color object.
:returns: An UIColor lightened with an amount of 0,2.
:returns: An UIColor lightened with an amount of 0.2.
:see: lightenColor:
*/
@ -161,7 +161,7 @@ public extension UIColor {
/**
Creates and returns a darker color object.
:returns: An UIColor darkened with an amount of 0,2.
:returns: An UIColor darkened with an amount of 0.2.
:see: darkenColor:
*/
@ -185,7 +185,7 @@ public extension UIColor {
/**
Creates and return a color object with the saturation increased by the given amount.
:returns: A UIColor more saturated with an amount of 0,2.
:returns: A UIColor more saturated with an amount of 0.2.
:see: saturateColor:
*/
@ -209,7 +209,7 @@ public extension UIColor {
/**
Creates and return a color object with the saturation decreased by the given amount.
:returns: A UIColor less saturated with an amount of 0,2.
:returns: A UIColor less saturated with an amount of 0.2.
:see: desaturateColor:
*/
@ -220,7 +220,7 @@ public extension UIColor {
/**
Creates and return a color object with the saturation decreased by the given amount.
:params: amount Float between 0 and 1. The default amount is equal to 0,2.
:params: amount Float between 0 and 1. The default amount is equal to 0.2.
:returns: A UIColor less saturated.
*/
@ -250,8 +250,8 @@ public extension UIColor {
*/
public func invertColor() -> UIColor {
var red: CGFloat = 0
var blue: CGFloat = 0
var green: CGFloat = 0
var blue: CGFloat = 0
var alpha: CGFloat = 0
if getRed(&red, green: &green, blue: &blue, alpha: &alpha) {
@ -260,4 +260,42 @@ public extension UIColor {
return self
}
// MARK: - Mixing Colors
/**
Mixes the given color object with the receiver.
Specifically, takes the average of each of the RGB components, optionally weighted by the given percentage. The opacity of the colors object is also considered when weighting the components.
:params: color A color object to mix with the receiver.
:params: weight The weight specifies the amount of the given color object. The default value is 0.5, means that half the given color and half the receiver color object should be used. 0.25 means that a quarter of the given color object and three quarters of the receiver color object should be used.
:returns: A color object corresponding to the two colors object mixed together.
*/
public func mixWithColor(color: UIColor, weight: CGFloat = 0.5) -> UIColor {
var red1: CGFloat = 0
var green1: CGFloat = 0
var blue1: CGFloat = 0
var alpha1: CGFloat = 0
var red2: CGFloat = 0
var green2: CGFloat = 0
var blue2: CGFloat = 0
var alpha2: CGFloat = 0
if getRed(&red1, green: &green1, blue: &blue1, alpha: &alpha1)
&& color.getRed(&red2, green: &green2, blue: &blue2, alpha: &alpha2) {
let w = 2 * weight - 1
let a = alpha1 - alpha2
let w2 = (((w * a == -1) ? w : (w + a) / (1 + w * a)) + 1) / 2
let w1 = 1 - w2
return UIColor(red: (w1 * red1 + w2 * red2), green: (w1 * green1 + w2 * green2), blue: (w1 * blue1 + w2 * blue2), alpha: (alpha1 * weight + alpha2 * (1 - weight)))
}
return self
}
}

View File

@ -53,6 +53,15 @@ class ViewController: UIViewController, UICollectionViewDataSource {
let invert = ("Invert", mainColor.invertColor())
_colors.append(invert)
let mixBlue = ("Mix Blue", mainColor.mixWithColor(UIColor.blueColor()))
_colors.append(mixBlue)
let mixGreen = ("Mix Green", mainColor.mixWithColor(UIColor.greenColor()))
_colors.append(mixGreen)
let mixYellow = ("Mix Yellow", mainColor.mixWithColor(UIColor.yellowColor()))
_colors.append(mixYellow)
colors = _colors
}

View File

@ -39,17 +39,17 @@ These will adjust the saturation of the color object, much like `darkenColor` an
</p>
```swift
let color = UIColor(hex: 0xc0392b)
let originalColor = UIColor(hex: 0xc0392b)
let saturated = color.saturatedColor()
let saturatedColor = originalColor.saturatedColor()
// equivalent to
// darker = color.saturateColor(0.2)
// saturatedColor = originalColor.saturateColor(0.2)
let desaturated = color.desaturatedColor()
let desaturatedColor = originalColor.desaturatedColor()
// equivalent to
// darker = color.desaturateColor(0.2)
// desaturatedColor = originalColor.desaturateColor(0.2)
let grayscale = color.grayscaledColor()
let grayscaleColor = originalColor.grayscaledColor()
```
#### Adjust-hue & Complement
@ -61,11 +61,11 @@ These adjust the hue value of the color in the same way like the others do. Agai
</p>
```swift
let color = UIColor(hex: 0xc0392b)
let originalColor = UIColor(hex: 0xc0392b)
let adjustHue = color.adjustedHueColor(45 / 360)
let adjustHueColor = originalColor.adjustedHueColor(45 / 360)
let complement = color.complementColor()
let complementColor = originalColor.complementColor()
````
#### Invert
@ -77,9 +77,9 @@ This can invert the color object. The red, green, and blue values are inverted,
</p>
```swift
let color = UIColor(hex: 0xc0392b)
let originalColor = UIColor(hex: 0xc0392b)
color.invertColor()
let invertColor = originalColor.invertColor()
```
To go further, take a look at the example project.

View File

@ -166,4 +166,25 @@ class DynamicColorTests: XCTestCase {
XCTAssert(invert.isEqualToHexString("#00ffff"), "Color string should equal to #00ffff")
XCTAssert(original.isEqualToHexString("#ff0000"), "Color string should equal to #ff0000")
}
func testMixColors() {
let red = UIColor.redColor()
let blue = UIColor.blueColor()
var midMix = red.mixWithColor(blue)
XCTAssert(midMix.isEqualToHexString("#7f007f"), "Color string should equal to #7f007f")
midMix = blue.mixWithColor(red)
XCTAssert(midMix.isEqualToHexString("#7f007f"), "Color string should equal to #7f007f")
let noMix = red.mixWithColor(blue, weight: 0)
XCTAssert(noMix.isEqualToHexString("#ff0000"), "Color string should equal to #ff0000")
let fullMix = red.mixWithColor(blue, weight: 1)
XCTAssert(fullMix.isEqualToHexString("#0000ff"), "Color string should equal to #0000ff")
}
}