Go to file
1024jp ee0bec946e
Update timing to run CodeQL
2023-05-01 17:30:03 +09:00
.github Update timing to run CodeQL 2023-05-01 17:30:03 +09:00
Sources/ColorCode Remove extra blank lines 2023-04-30 10:58:21 +09:00
Tests/ColorCodeTests Update unit tests 2023-04-30 10:58:20 +09:00
.gitignore Minimize .gitignore 2020-02-27 19:26:45 +09:00
.swiftlint.yml Style code 2022-02-04 17:10:32 +09:00
CHANGELOG.md Bump up supported macOS version to 11 2022-09-23 23:20:45 +09:00
LICENSE Update copyright year 2023-04-30 10:31:35 +09:00
Package.swift Bump up supported macOS version to 11 2022-09-23 23:20:45 +09:00
README.md Update copyright year 2023-04-30 10:31:35 +09:00

README.md

WFColorCode

Test Status SwiftPM compatible

WFColorCode is a set of NSColor extensions that allows creating NSColor/SwiftUI.Color instance from a CSS color code string, or color code string from an NSColor/SwiftUI.Color instance. It also adds the ability to handle HSL color space.

  • Requirements: macOS 11 or later

Usage

WFColorCode supports the following color code styles.

/// color code type
enum ColorCodeType: Int {
    case hex        // #ffffff
    case shortHex   // #fff
    case cssRGB     // rgb(255,255,255)
    case cssRGBa    // rgba(255,255,255,1)
    case cssHSL     // hsl(0,0%,100%)
    case cssHSLa    // hsla(0,0%,100%,1)
    case cssKeyword // White
};

Example

Import ColorCode to use.

import ColorCode

// create NSColor instance from HSLa color code
var type: ColorCodeType?
let whiteColor = NSColor(colorCode: "hsla(0,0%,100%,0.5)", type: &type)
let hex: String = whiteColor.colorCode(type: .hex)  // => "#ffffff"

// create NSColor instance from HSLa values
let color = NSColor(deviceHue:0.1, saturation:0.2, lightness:0.3, alpha:1.0)

// create NSColor instance from a CSS3 keyword
let ivoryColor = NSColor(colorCode: "ivory")

// get HSL values from NSColor instance
var hue: CGFloat = 0
var saturation: CGFloat = 0
var lightness: CGFloat = 0
var alpha: CGFloat = 0
color.getHue(hue: &hue, saturation: &saturation, lightness: &lightness, alpha: &alpha)

License

© 2014-2023 1024jp.

The source code is distributed under the terms of the MIT License. See the bundled LICENSE for details.