Add the toRGBA and toABGR methods #50
This commit is contained in:
parent
79048e2f8e
commit
0dfcf55012
14
CHANGELOG.md
14
CHANGELOG.md
|
@ -3,13 +3,13 @@
|
|||
## [Version 4.1.0](https://github.com/yannickl/DynamicColor/releases/tag/4.1.0)
|
||||
*Released on 2019-05-27.*
|
||||
|
||||
- Minor improvements (compilation performance, documentation)
|
||||
- [FIX] Minor improvements (compilation performance, documentation)
|
||||
- Swift 5/Xcode 10 support
|
||||
|
||||
## [Version 4](https://github.com/yannickl/DynamicColor/releases/tag/4)
|
||||
*Released on 2017-11-07.*
|
||||
|
||||
- Minor improvements (compilation performance, documentation)
|
||||
- [FIX] Minor improvements (compilation performance, documentation)
|
||||
- Swift 4/Xcode 9 support
|
||||
|
||||
## [Version 3.3](https://github.com/yannickl/DynamicColor/releases/tag/3.3)
|
||||
|
@ -33,12 +33,12 @@
|
|||
*Released on 2016-09-08.*
|
||||
|
||||
- [ADD] CIE XYZ Color Space
|
||||
- Initialization with XYZ components
|
||||
- `toXYZComponents()` method
|
||||
- [ADD] Initialization with XYZ components
|
||||
- [ADD] `toXYZComponents()` method
|
||||
- [ADD] CIE L*a*b* Color Space
|
||||
- Initialization with L*a*b* components
|
||||
- `toLabComponents()` method
|
||||
- `toHSBComponents()` method
|
||||
- [ADD] Initialization with L*a*b* components
|
||||
- [ADD] `toLabComponents()` method
|
||||
- [ADD] `toHSBComponents()` method
|
||||
- [REFACTORING] `toHSLAComponents` to `toHSLComponents`
|
||||
- [REFACTORING] Hue range is now from 0° and 360° instead of 0.0 and 1.0
|
||||
- [ADD] `DynamicGradient` object
|
||||
|
|
|
@ -105,22 +105,36 @@ public extension DynamicColor {
|
|||
}
|
||||
|
||||
/**
|
||||
Returns the color representation as an integer.
|
||||
Returns the color representation as an integer (without the alpha channel).
|
||||
|
||||
- returns: A UInt32 that represents the hexa-decimal color.
|
||||
*/
|
||||
final func toHex() -> UInt32 {
|
||||
func roundToHex(_ x: CGFloat) -> UInt32 {
|
||||
guard x > 0 else { return 0 }
|
||||
let rounded: CGFloat = round(x * 255)
|
||||
let rgba = toRGBAComponents()
|
||||
|
||||
return UInt32(rounded)
|
||||
return roundToHex(rgba.r) << 16 | roundToHex(rgba.g) << 8 | roundToHex(rgba.b)
|
||||
}
|
||||
|
||||
let rgba = toRGBAComponents()
|
||||
let colorToInt = roundToHex(rgba.r) << 16 | roundToHex(rgba.g) << 8 | roundToHex(rgba.b)
|
||||
/**
|
||||
Returns the RGBA color representation.
|
||||
|
||||
return colorToInt
|
||||
- returns: A UInt32 that represents the color as an RGBA value.
|
||||
*/
|
||||
func toRGBA() -> UInt32 {
|
||||
let rgba = toRGBAComponents()
|
||||
|
||||
return roundToHex(rgba.r) << 24 | roundToHex(rgba.g) << 16 | roundToHex(rgba.b) << 8 | roundToHex(rgba.a)
|
||||
}
|
||||
|
||||
/**
|
||||
Returns the AGBR color representation.
|
||||
|
||||
- returns: A UInt32 that represents the color as an AGBR value.
|
||||
*/
|
||||
func toAGBR() -> UInt32 {
|
||||
let rgba = toRGBAComponents()
|
||||
|
||||
return roundToHex(rgba.a) << 24 | roundToHex(rgba.b) << 16 | roundToHex(rgba.g) << 8 | roundToHex(rgba.r)
|
||||
}
|
||||
|
||||
// MARK: - Identifying and Comparing Colors
|
||||
|
|
|
@ -124,13 +124,13 @@ internal struct HSL {
|
|||
return m1 + ((m2 - m1) * hue * 6)
|
||||
}
|
||||
else if hue * 2 < 1 {
|
||||
return CGFloat(m2)
|
||||
return m2
|
||||
}
|
||||
else if hue * 3 < 1.9999 {
|
||||
return m1 + ((m2 - m1) * (2 / 3 - hue) * 6)
|
||||
}
|
||||
|
||||
return CGFloat(m1)
|
||||
return m1
|
||||
}
|
||||
|
||||
// MARK: - Deriving the Color
|
||||
|
|
|
@ -64,3 +64,10 @@ internal func moda(_ x: CGFloat, m: CGFloat) -> CGFloat {
|
|||
internal func roundDecimal(_ x: CGFloat, precision: CGFloat = 10000) -> CGFloat {
|
||||
return CGFloat(Int(round(x * precision))) / precision
|
||||
}
|
||||
|
||||
internal func roundToHex(_ x: CGFloat) -> UInt32 {
|
||||
guard x > 0 else { return 0 }
|
||||
let rounded: CGFloat = round(x * 255)
|
||||
|
||||
return UInt32(rounded)
|
||||
}
|
||||
|
|
|
@ -84,6 +84,38 @@ class DynamicColorTests: XCTestCase {
|
|||
XCTAssert(custom.toHex() == 0x769a2b, "Color string should be equal to 0x769a2b")
|
||||
}
|
||||
|
||||
func testToRGBA() {
|
||||
let red = DynamicColor.red
|
||||
let blue = DynamicColor.blue
|
||||
let green = DynamicColor.green
|
||||
let yellow = DynamicColor.yellow
|
||||
let custom = DynamicColor(hex: 0x769a2b)
|
||||
let yellowWithAlpha = DynamicColor.yellow.adjustedAlpha(amount: -0.5)
|
||||
|
||||
XCTAssert(red.toRGBA() == 0xff0000ff, "Color string should be equal to 0xff0000ff")
|
||||
XCTAssert(blue.toRGBA() == 0x0000ffff, "Color string should be equal to 0x0000ffff")
|
||||
XCTAssert(green.toRGBA() == 0x00ff00ff, "Color string should be equal to 0x00ff00ff")
|
||||
XCTAssert(yellow.toRGBA() == 0xffff00ff, "Color string should be equal to 0xffff00ff")
|
||||
XCTAssert(custom.toRGBA() == 0x769a2bff, "Color string should be equal to 0x769a2bff")
|
||||
XCTAssert(yellowWithAlpha.toRGBA() == 0xffff0080, "%d Color string should be equal to 0xffff0080")
|
||||
}
|
||||
|
||||
func testToAGBR() {
|
||||
let red = DynamicColor.red
|
||||
let blue = DynamicColor.blue
|
||||
let green = DynamicColor.green
|
||||
let yellow = DynamicColor.yellow
|
||||
let custom = DynamicColor(hex: 0x769a2b)
|
||||
let yellowWithAlpha = DynamicColor.yellow.adjustedAlpha(amount: -0.5)
|
||||
|
||||
XCTAssert(red.toAGBR() == 0xff0000ff, "Color string should be equal to 0xff0000ff")
|
||||
XCTAssert(blue.toAGBR() == 0xffff0000, "Color string should be equal to 0xffff0000")
|
||||
XCTAssert(green.toAGBR() == 0xff00ff00, "Color string should be equal to 0xff00ff00")
|
||||
XCTAssert(yellow.toAGBR() == 0xff00ffff, "Color string should be equal to 0xff00ffff")
|
||||
XCTAssert(custom.toAGBR() == 0xff2b9a76, "Color string should be equal to 0xff2b9a76")
|
||||
XCTAssert(yellowWithAlpha.toAGBR() == 0x8000ffff, "%d Color string should be equal to 0x8000ffff")
|
||||
}
|
||||
|
||||
func testToHexWithAlpha() {
|
||||
let custom = DynamicColor(hex: 0x769a2b80, useAlpha: true)
|
||||
|
||||
|
|
Loading…
Reference in New Issue