Giter Site home page Giter Site logo

wfcolorcode's Introduction

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 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.

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 for details.

wfcolorcode's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.