DynamicColor

repository·master·Indexed 25 days ago

https://github.com/yannickl/dynamiccolor

A Swift library for color manipulation in UIKit and SwiftUI. It provides tools for adjusting lightness, saturation, and hue, as well as methods for creating tints, shades, inverted colors, and mixed colors. Includes support for creating colors from hex strings and generating color palettes via DynamicGradient across various color spaces such as RGB, HSL, HSB, and LAB.

Tokens
1.3K
Snippets
7
Records
11
Agent score
35%

What's inside DynamicColor

  1. Install DynamicColor via Swift Package Manager

    master

    Add DynamicColor as a dependency in your Package.swift file:

    .package(url: "https://github.com/yannickl/DynamicColor.git", from: "5.0.0")
    import PackageDescription
    
    let package = Package(
        name: "YOUR_PROJECT_NAME",
        targets: [],
        dependencies: [
            .package(url: "https://github.com/yannickl/DynamicColor.git", from: "5.0.0")
        ]
    )
  2. Install DynamicColor via CocoaPods

    master

    To install DynamicColor using CocoaPods, add the following to your Podfile:

    use_frameworks!
    pod 'DynamicColor', '~> 5.0.0'

    Then, run pod install in your project directory and open the .xcworkspace file.

    $ cd /path/to/MyProject
    $ touch Podfile
    $ edit Podfile
    source 'https://github.com/CocoaPods/Specs.git'
    platform :ios, '8.0'
    
    use_frameworks!
    pod 'DynamicColor', '~> 5.0.0'
    
    $ pod install
  3. Adjust color saturation and grayscale

    master

    Adjust saturation using .saturated(amount:) and .desaturated(amount:). You can also convert a color to grayscale using .grayscaled(mode:) with several modes:

    • .luminance
    • .lightness
    • .average
    • .value
    let originalColor = DynamicColor(hexString: "#c0392b")
    
    let saturatedColor = originalColor.saturated(amount: 0.2)
    let desaturatedColor = originalColor.desaturated(amount: 0.2)
    
    let grayscaledColor = originalColor.grayscaled()
    let grayscaledColorLuminance = originalColor.grayscaled(mode: .luminance)
    let grayscaledColorLightness = originalColor.grayscaled(mode: .lightness)
    let grayscaledColorAverage = originalColor.grayscaled(mode: .average)
    let grayscaledColorValue = originalColor.grayscaled(mode: .value)
  4. Generate color palettes using DynamicGradient

    master

    The DynamicGradient object allows you to create color palettes from a set of colors across different color spaces like .rgb, .hsl, .hsb, or .lab.

    let blue   = UIColor(hexString: "#3498db")
    let red    = UIColor(hexString: "#e74c3c")
    let yellow = UIColor(hexString: "#f1c40f")
    
    let gradient = DynamicGradient(colors: [blue, red, yellow])
    
    // RGB palette (default)
    let rgbPalette = gradient.colorPalette(amount: 8)
    
    // HSL palette
    let hslPalette = gradient.colorPalette(amount: 8, inColorSpace: .hsl)
    
    // Lab palette using array extension
    let labPalette = [blue, red, yellow].gradient.colorPalette(amount: 8, inColorSpace: .lab)
  5. Adjust hue and find complement colors

    master

    Use .adjustedHue(amount:) to change the hue (amount is in degrees) and .complemented() to find the complementary color.

    let originalColor = DynamicColor(hex: 0xc0392b)
    
    // Hue values are in degrees
    let adjustHueColor = originalColor.adjustedHue(amount: 45)
    let complementedColor = originalColor.complemented()
  6. Create colors using Hex strings or values

    master
    DynamicColor provides initializers to create colors from hex strings or integer values. You can use UIColor(hexString:) or UIColor(hex:). For platform independence (iOS/watchOS/tvOS vs macOS), use the DynamicColor typealias.