Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Hex string color code and int with alpha value #10

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ jobs:
strategy:
matrix:
env:
- sdk: iphonesimulator12.2
destination: platform=iOS Simulator,name=iPhone XS,OS=12.2
- sdk: iphonesimulator12.4
destination: platform=iOS Simulator,name=iPhone XS,OS=12.4

- sdk: macosx10.14
destination: arch=x86_64
Expand Down
7 changes: 7 additions & 0 deletions .swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 20 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,32 @@ SwiftyColor

The most sexy way to use colors in Swift. Both compatible with iOS and macOS.

Color from Hex
Color from Hex int
--------------

```swift
//RGB
let color = 0x123456.color
```

```swift
//ARGB
let colorWithAlpha = 0xFF123456.color
```

Color from Hex string color code
--------------
```swift
//#RGB
let color = "#123456".color
```

```swift
//#ARGB
let colorWithAlpha = "#FF123456".color
```


Alpha Operator
--------------

Expand Down
30 changes: 30 additions & 0 deletions Sources/SwiftyColor/SwiftyColor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,43 @@

extension Int {
public var color: Color {
if self > 16777215 {//ARGB
let alpha = CGFloat(self as Int >> 24 & 0xff) / 255
let red = CGFloat(self as Int >> 16 & 0xff) / 255
let green = CGFloat(self >> 8 & 0xff) / 255
let blue = CGFloat(self & 0xff) / 255
return Color(red: red, green: green, blue: blue, alpha: alpha)
}

//RGB
let red = CGFloat(self as Int >> 16 & 0xff) / 255
let green = CGFloat(self >> 8 & 0xff) / 255
let blue = CGFloat(self & 0xff) / 255
return Color(red: red, green: green, blue: blue, alpha: 1)
}
}

extension String{

public var color: Color?{
var colorString:String = self.trimmingCharacters(in: .whitespacesAndNewlines).uppercased()
let validatePattern = "^[#]([0-9A-F]{6}|[0-9A-F]{8})$"
let colorPred = NSPredicate(format:"SELF MATCHES %@", validatePattern)
let isMatch = colorPred.evaluate(with: self)
if isMatch{
colorString.remove(at: colorString.startIndex)
if colorString.count == 6{ //if color code is RGB add FF as prefix to convert to ARGB
colorString = "FF\(colorString)"
}
var rgbValue:UInt64 = 0
Scanner(string: colorString).scanHexInt64(&rgbValue)
return Int(rgbValue).color
}
return nil
}

}

precedencegroup AlphaPrecedence {
associativity: left
higherThan: RangeFormationPrecedence
Expand Down
68 changes: 51 additions & 17 deletions Tests/SwiftyColorTests/SwiftyColorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,32 +24,66 @@
// SOFTWARE.

#if os(iOS)
import UIKit
import UIKit
#elseif os(OSX)
import AppKit
import AppKit
#endif

import XCTest
import SwiftyColor

final class SwiftyColorTests: XCTestCase {

func testHex() {
let color = 0x123456.color
var (red, green, blue) = (CGFloat(), CGFloat(), CGFloat())
color.getRed(&red, green: &green, blue: &blue, alpha: nil)
XCTAssert(Int(red * 255) == 0x12)
XCTAssert(Int(green * 255) == 0x34)
XCTAssert(Int(blue * 255) == 0x56)
}
func testHexRGB() {
let color = 0x123456.color
var (red, green, blue, alpha) = (CGFloat(), CGFloat(), CGFloat(), CGFloat())
color.getRed(&red, green: &green, blue: &blue, alpha: &alpha)
XCTAssert(Int(alpha * 255) == 0xFF)
XCTAssert(Int(red * 255) == 0x12)
XCTAssert(Int(red * 255) == 0x12)
XCTAssert(Int(green * 255) == 0x34)
XCTAssert(Int(blue * 255) == 0x56)
}

func testHexARGB() {
let color = 0x90123456.color
var (red, green, blue, alpha) = (CGFloat(), CGFloat(), CGFloat(), CGFloat())
color.getRed(&red, green: &green, blue: &blue, alpha: &alpha)
XCTAssert(Int(alpha * 255) == 0x90)
XCTAssert(Int(red * 255) == 0x12)
XCTAssert(Int(green * 255) == 0x34)
XCTAssert(Int(blue * 255) == 0x56)
}

func testAlpha() {
XCTAssertEqual(0x123456.color ~ 50%, 0x123456.color.withAlphaComponent(0.5))
}
func testAlpha() {
XCTAssertEqual(0x123456.color ~ 50% , 0x123456.color.withAlphaComponent(0.5))
}

func testPercent() {
XCTAssert(50% == 0.5)
XCTAssert(12% == 0.12)
}
func testPercent() {
XCTAssert(50% == 0.5)
XCTAssert(12% == 0.12)
}

func testStringColorRGB() {
let color = "#123456".color
XCTAssertNotNil(color)
var (red, green, blue, alpha) = (CGFloat(), CGFloat(), CGFloat(), CGFloat())
color!.getRed(&red, green: &green, blue: &blue, alpha: &alpha)
XCTAssert(Int(alpha * 255) == 0xFF)
XCTAssert(Int(red * 255) == 0x12)
XCTAssert(Int(green * 255) == 0x34)
XCTAssert(Int(blue * 255) == 0x56)
}

func testStringColorARGB() {
let color = "#90123456".color
XCTAssertNotNil(color)
var (red, green, blue, alpha) = (CGFloat(), CGFloat(), CGFloat(), CGFloat())
color!.getRed(&red, green: &green, blue: &blue, alpha: &alpha)
XCTAssert(Int(alpha * 255) == 0x90)
XCTAssert(Int(red * 255) == 0x12)
XCTAssert(Int(green * 255) == 0x34)
XCTAssert(Int(blue * 255) == 0x56)
}

}