https://github.com/1024jp/wfcolorcode
NSColor extension adding ability to handle HSL color space and CSS3 style color codes
https://github.com/1024jp/wfcolorcode
cocoa nscolor swift
Last synced: about 1 year ago
JSON representation
NSColor extension adding ability to handle HSL color space and CSS3 style color codes
- Host: GitHub
- URL: https://github.com/1024jp/wfcolorcode
- Owner: 1024jp
- License: mit
- Created: 2014-09-02T23:20:03.000Z (almost 12 years ago)
- Default Branch: main
- Last Pushed: 2024-10-20T13:08:36.000Z (over 1 year ago)
- Last Synced: 2025-04-06T01:06:39.142Z (about 1 year ago)
- Topics: cocoa, nscolor, swift
- Language: Swift
- Homepage:
- Size: 191 KB
- Stars: 30
- Watchers: 2
- Forks: 9
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
ColorCode
=============================
__ColorCode__ 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 14 or later
Usage
-----------------------------
WFColorCode supports the following color code styles.
```swift
/// color code type
enum ColorCodeType: Int {
case hex // #ffffff
case hexWithAlpha // #ffffffff
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.
```swift
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-2024 1024jp.
The source code is distributed under the terms of the __MIT License__. See the bundled [LICENSE](LICENSE) for details.