Git Product home page Git Product logo

color's Introduction

Color

Version Build Status Swift Version Carthage compatible

Color utilities in pure Swift for macOS, iOS, watchOS, and tvOS. Color supports Carthage and Swift Package Manager. It is also tested on Linux.

Color was abstracted from Contrast, a macOS app for checking designs for accessible color combinations.

Installation

Carthage

Add the following to your Cartfile:

github "soffes/Color"

Swift Package Manager

Add the following to your dependencies in your Package.swift:

.package(url: "https://github.com/soffes/Color.git", from: "0.1.1")

Usage

Color provides structs for different color models. Currently only RGB and HSL are supported. Most color calculations are specific to a color model. For example, darkening a color is only available for HSL colors and not RGB colors. If you'd like to darken an RGB color, you’ll need to convert to HSL first. Here’s an example:

let rgb = RGBColor(red: 1, green: 0, blue: 0)
let hsl = HSLColor(rgb: rgb)
let darkerRed = hsl.darkening()

Note that Color’s structs don’t support alpha since that isn’t used in any of the color calculations. This may be added in the future.

Hex conversions are only available for RGB colors.

let rgb = RGBColor(hex: "0f0")!
rgb.hex // "00ff00"

NSColor & UIColor

There are convenience methods on NSColor & UIColor if you are working in a Cocoa application. Each color model (RGBColor, HSLColor, etc.) has an extension for SystemColorType which is either NSColor or UIColor depending on the platform. Here's a few examples:

UIColor.red.darkening()
UIColor.green.desaturating()
NSColor.blue.isDark

WCAG

RGB colors also have WCAG 2.0 calculations. First, calculate a contrast ratio:

let color1 = RGBColor(red: 170.0 / 255.0, green: 204.0 / 255.0, blue: 1)
let color2 = RGBColor(red: 34.0 / 255.0, green: 34.0 / 255.0, blue: 51.0 / 255.0)
let contrastRatio = color1.contrastRatio(to: color2) // 9.51

Now, you can check the conformance level:

ConformanceLevel(contrastRatio: contrastRatio) // .aaa

Thanks

Thanks to color & color-convert for inspiration.

color's People

Contributors

soffes avatar zef avatar

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