### Install DynamicColor with CocoaPods Source: https://github.com/yannickl/dynamiccolor/blob/master/README.md Install CocoaPods, then add `pod 'DynamicColor', '~> 5.0.0'` to your Podfile and run `pod install`. Ensure you open the `.xcworkspace` file afterwards. ```bash $ [sudo] gem install cocoapods $ pod setup ``` ```ruby source 'https://github.com/CocoaPods/Specs.git' platform :ios, '8.0' use_frameworks! pod 'DynamicColor', '~> 5.0.0' ``` ```bash $ cd /path/to/MyProject $ touch Podfile $ edit Podfile ``` ```bash $ pod install ``` ```bash $ open MyProject.xcworkspace ``` -------------------------------- ### Install DynamicColor with Carthage Source: https://github.com/yannickl/dynamiccolor/blob/master/README.md Install Carthage using Homebrew. Then, add `github "yannickl/DynamicColor" >= 5.0.0` to your `Cartfile`. ```bash $ brew update $ brew install carthage ``` ```ogdl github "yannickl/DynamicColor" >= 5.0.0 ``` -------------------------------- ### Install DynamicColor with Swift Package Manager Source: https://github.com/yannickl/dynamiccolor/blob/master/README.md Add this dependency to your Package.swift file to install DynamicColor using Swift Package Manager. Ensure you are using a compatible version. ```swift import PackageDescription let package = Package( name: "YOUR_PROJECT_NAME", targets: [], dependencies: [ .package(url: "https://github.com/yannickl/DynamicColor.git", from: "5.0.0") ] ) ``` -------------------------------- ### Adjust Hue and Complement Colors Source: https://github.com/yannickl/dynamiccolor/blob/master/README.md Modify the hue of a color or get its complementary color. Hue values are in degrees. ```swift let originalColor = DynamicColor(hex: 0xc0392b) // Hue values are in degrees let adjustHueColor = originalColor.adjustedHue(amount: 45) let complementedColor = originalColor.complemented() ``` -------------------------------- ### Initialize DynamicColor from HSL Components Source: https://context7.com/yannickl/dynamiccolor/llms.txt Creates a color directly from Hue (0-360°), Saturation (0.0-1.0), and Lightness (0.0-1.0) components. Also shows how to retrieve these components back from an existing color. ```swift // Hue in degrees (0-360), saturation and lightness as 0.0-1.0 let skyBlue = DynamicColor(hue: 204, saturation: 0.64, lightness: 0.53) let coral = DynamicColor(hue: 16, saturation: 0.78, lightness: 0.63) let forest = DynamicColor(hue: 120, saturation: 0.39, lightness: 0.32, alpha: 0.9) // Retrieve HSL components back let hsl = skyBlue.toHSLComponents() // hsl.h = 204.0, hsl.s = 0.64, hsl.l ≈ 0.53 print("Hue: \(hsl.h)°, Saturation: \(hsl.s), Lightness: \(hsl.l)") // Hue: 204.0°, Saturation: 0.64, Lightness: 0.53 ``` -------------------------------- ### Create and Sample Colors from a DynamicGradient Source: https://context7.com/yannickl/dynamiccolor/llms.txt Initialize a DynamicGradient with an array of DynamicColor objects. Use `pickColorAt(scale:)` to retrieve colors at specific points in the gradient (0.0 to 1.0). For perceptually smoother blending, specify a color space like .lab. ```swift let blue = DynamicColor(hexString: "#3498db") let red = DynamicColor(hexString: "#e74c3c") let yellow = DynamicColor(hexString: "#f1c40f") // Create a gradient let gradient = DynamicGradient(colors: [blue, red, yellow]) // Also available as array property: // let gradient = [blue, red, yellow].gradient // Pick a color at a specific position (0.0 = blue, 0.5 = red, 1.0 = yellow) let start = gradient.pickColorAt(scale: 0.0) // → blue let midway = gradient.pickColorAt(scale: 0.5) // → red let quarter = gradient.pickColorAt(scale: 0.25) // → between blue and red let end = gradient.pickColorAt(scale: 1.0) // → yellow // Pick with a different color space for perceptually smoother blending let labMid = gradient.pickColorAt(scale: 0.5, inColorSpace: .lab) ``` ```swift // Generate a palette of N equidistant colors let rgbPalette = gradient.colorPalette(amount: 8) // RGB (default) let hslPalette = gradient.colorPalette(amount: 8, inColorSpace: .hsl) let labPalette = gradient.colorPalette(amount: 8, inColorSpace: .lab) let hsbPalette = gradient.colorPalette(amount: 8, inColorSpace: .hsb) // Use palette for a chart's data range coloring let dataRange = [blue, yellow].gradient.colorPalette(amount: 10) // dataRange[0] = coldest/lowest, dataRange[9] = hottest/highest ``` -------------------------------- ### Initialize DynamicColor from CIE L*a*b* Components Source: https://context7.com/yannickl/dynamiccolor/llms.txt Creates a color from perceptually-uniform CIE L*a*b* coordinates. Shows conversion to L*a*b* components and calculating perceptual color distance (ΔE). ```swift // Initialize from L*a*b* components let warmWhite = DynamicColor(L: 97.0, a: 1.5, b: 5.0) let richBlack = DynamicColor(L: 5.0, a: 0.0, b: 0.0) // Convert a color to L*a*b* components let skyBlue = DynamicColor(hexString: "#87CEEB") let lab = skyBlue.toLabComponents() // lab.L ≈ 82.9, lab.a ≈ -9.5, lab.b ≈ -19.2 // Useful for computing perceptual color distance (ΔE) let color1 = DynamicColor(hexString: "#e74c3c") let color2 = DynamicColor(hexString: "#c0392b") let lab1 = color1.toLabComponents() let lab2 = color2.toLabComponents() let deltaE = sqrt(pow(lab1.L - lab2.L, 2) + pow(lab1.a - lab2.a, 2) + pow(lab1.b - lab2.b, 2)) // deltaE ≈ perceptual distance between the two colors ``` -------------------------------- ### Initialize DynamicColor from CIE XYZ Components Source: https://context7.com/yannickl/dynamiccolor/llms.txt Creates a color from CIE XYZ coordinates using a 2° observer and D65 illuminant. Also demonstrates converting any color to its XYZ components. ```swift // Initialize from XYZ (observer 2°, D65 illuminant) let xyzColor = DynamicColor(X: 41.24, Y: 21.26, Z: 1.93) // approximately red // Convert any color to XYZ components let red = DynamicColor(hexString: "#e74c3c") let xyz = red.toXYZComponents() // xyz.X ≈ 30.6, xyz.Y ≈ 17.1, xyz.Z ≈ 3.4 print("X: \(xyz.X), Y: \(xyz.Y), Z: \(xyz.Z)") ``` -------------------------------- ### Initialization from CIE L*a*b* Components Source: https://context7.com/yannickl/dynamiccolor/llms.txt Creates a color from perceptually-uniform CIE L*a*b* coordinates (L: 0–100, a/b: -128 to 127), enabling color differences that match human perception. ```APIDOC ## Initialization from CIE L*a*b* Components Creates a color from perceptually-uniform CIE L*a*b* coordinates (L: 0–100, a/b: -128 to 127), enabling color differences that match human perception. ```swift // Initialize from L*a*b* components let warmWhite = DynamicColor(L: 97.0, a: 1.5, b: 5.0) let richBlack = DynamicColor(L: 5.0, a: 0.0, b: 0.0) // Convert a color to L*a*b* components let skyBlue = DynamicColor(hexString: "#87CEEB") let lab = skyBlue.toLabComponents() // lab.L ≈ 82.9, lab.a ≈ -9.5, lab.b ≈ -19.2 // Useful for computing perceptual color distance (ΔE) let color1 = DynamicColor(hexString: "#e74c3c") let color2 = DynamicColor(hexString: "#c0392b") let lab1 = color1.toLabComponents() let lab2 = color2.toLabComponents() let deltaE = sqrt(pow(lab1.L - lab2.L, 2) + pow(lab1.a - lab2.a, 2) + pow(lab1.b - lab2.b, 2)) // deltaE ≈ perceptual distance between the two colors ``` ``` -------------------------------- ### Initialization from RGB Components (0–255 Range) Source: https://context7.com/yannickl/dynamiccolor/llms.txt Initializes a color using 8-bit RGB values (0–255) instead of the standard 0.0–1.0 range. Values outside the range are automatically clipped. ```APIDOC ## Initialization from RGB Components (0–255 Range) Initializes a color using 8-bit RGB values (0–255) instead of the standard 0.0–1.0 range. Values outside the range are automatically clipped. ```swift // Using 0-255 component values (more intuitive for designers) let orange = DynamicColor(r: 230, g: 126, b: 34) let purple = DynamicColor(r: 155, g: 89, b: 182, a: 200) // with alpha // Compare with standard UIColor init (requires 0.0-1.0 range) let orangeStandard = UIColor(red: 230/255, green: 126/255, blue: 34/255, alpha: 1.0) ``` ``` -------------------------------- ### Initialization from HSL Components Source: https://context7.com/yannickl/dynamiccolor/llms.txt Creates a color directly from Hue (0–360°), Saturation (0.0–1.0), and Lightness (0.0–1.0) components, a more intuitive model for programmatic color design than RGB. ```APIDOC ## Initialization from HSL Components Creates a color directly from Hue (0–360°), Saturation (0.0–1.0), and Lightness (0.0–1.0) components, a more intuitive model for programmatic color design than RGB. ```swift // Hue in degrees (0-360), saturation and lightness as 0.0-1.0 let skyBlue = DynamicColor(hue: 204, saturation: 0.64, lightness: 0.53) let coral = DynamicColor(hue: 16, saturation: 0.78, lightness: 0.63) let forest = DynamicColor(hue: 120, saturation: 0.39, lightness: 0.32, alpha: 0.9) // Retrieve HSL components back let hsl = skyBlue.toHSLComponents() // hsl.h = 204.0, hsl.s = 0.64, hsl.l ≈ 0.53 print("Hue: \(hsl.h)°, Saturation: \(hsl.s), Lightness: \(hsl.l)") // Hue: 204.0°, Saturation: 0.64, Lightness: 0.53 ``` ``` -------------------------------- ### Initialization from CIE XYZ Components Source: https://context7.com/yannickl/dynamiccolor/llms.txt Creates a color from CIE XYZ coordinates using a 2° observer and D65 illuminant, useful for colorimetry and scientific color work. X: 0–95.05, Y: 0–100.0, Z: 0–108.9. ```APIDOC ## Initialization from CIE XYZ Components Creates a color from CIE XYZ coordinates using a 2° observer and D65 illuminant, useful for colorimetry and scientific color work. X: 0–95.05, Y: 0–100.0, Z: 0–108.9. ```swift // Initialize from XYZ (observer 2°, D65 illuminant) let xyzColor = DynamicColor(X: 41.24, Y: 21.26, Z: 1.93) // approximately red // Convert any color to XYZ components let red = DynamicColor(hexString: "#e74c3c") let xyz = red.toXYZComponents() // xyz.X ≈ 30.6, xyz.Y ≈ 17.1, xyz.Z ≈ 3.4 print("X: \(xyz.X), Y: \(xyz.Y), Z: \(xyz.Z)") ``` ``` -------------------------------- ### Initialization from Hex String or Integer Source: https://context7.com/yannickl/dynamiccolor/llms.txt Creates a DynamicColor (or SwiftUI Color) from a hex string or a hex integer literal. Invalid hex strings fall back to black. RGBA hex strings (8 characters with alpha) are also supported. ```APIDOC ## Initialization from Hex String or Integer Creates a `DynamicColor` (or SwiftUI `Color`) from a hex string or a hex integer literal. Invalid hex strings fall back to black. RGBA hex strings (8 characters with alpha) are also supported. ```swift import DynamicColor // From a hex string (with or without "#") let blue = DynamicColor(hexString: "#3498db") let red = DynamicColor(hexString: "#c0392b") // From an 8-character RGBA hex string (includes alpha) let semiTransparentRed = DynamicColor(hexString: "#FF0934CC") // From a hex integer let green = DynamicColor(hex: 0x2ecc71) // With explicit alpha channel in hex integer let fadedBlue = DynamicColor(hex: 0x3498dbCC, useAlpha: true) // Platform-independent usage (UIColor on iOS/tvOS/watchOS, NSColor on macOS) let color: DynamicColor = DynamicColor(hex: 0x3498db) // SwiftUI (iOS 13+, macOS 10.15+) import SwiftUI let swiftUIColor = Color(hexString: "#3498db") let swiftUIColor2 = Color(hex: 0x3498db) let swiftUIWithAlpha = Color(hex: 0x3498dbFF, useOpacity: true) ``` ``` -------------------------------- ### Initialize DynamicColor from RGB Components (0-255 Range) Source: https://context7.com/yannickl/dynamiccolor/llms.txt Initializes a color using 8-bit RGB values (0-255) for easier use by designers. Values outside this range are automatically clipped. This is an alternative to the standard 0.0-1.0 range initialization. ```swift // Using 0-255 component values (more intuitive for designers) let orange = DynamicColor(r: 230, g: 126, b: 34) let purple = DynamicColor(r: 155, g: 89, b: 182, a: 200) // with alpha // Compare with standard UIColor init (requires 0.0-1.0 range) let orangeStandard = UIColor(red: 230/255, green: 126/255, blue: 34/255, alpha: 1.0) ``` -------------------------------- ### Create DynamicGradient Object Source: https://github.com/yannickl/dynamiccolor/blob/master/README.md Initialize a `DynamicGradient` with an array of `UIColor` objects. Alternatively, use the `gradient` extension property on an array of colors. ```swift let blue = UIColor(hexString: "#3498db") let red = UIColor(hexString: "#e74c3c") let yellow = UIColor(hexString: "#f1c40f") let gradient = DynamicGradient(colors: [blue, red, yellow]) // equivalent to // let gradient = [blue, red, yellow].gradient ``` -------------------------------- ### Contrast Ratio and WCAG Accessibility Checks Source: https://context7.com/yannickl/dynamiccolor/llms.txt Computes the WCAG 2.0 contrast ratio between two colors and verifies whether they meet accessibility standards for a given display context. ```APIDOC ## Contrast Ratio and WCAG Accessibility Checks Computes the WCAG 2.0 contrast ratio between two colors and verifies whether they meet accessibility standards for a given display context. ```swift let background = DynamicColor(hexString: "#ffffff") // white let text = DynamicColor(hexString: "#767676") // medium gray let darkText = DynamicColor(hexString: "#595959") // darker gray // Compute contrast ratio (same result regardless of order) let ratio = background.contrastRatio(with: text) print(ratio) // ~4.48 (just below AA standard of 4.5) // Check WCAG compliance for different contexts // .standard → min 4.5:1 (AA, normal text) // .standardLargeText → min 3.0:1 (AA, large text ≥18pt or bold ≥14pt) // .enhanced → min 7.0:1 (AAA, normal text) // .enhancedLargeText → min 4.5:1 (AAA, large text) print(background.isContrasting(with: text, inContext: .standard)) // false (4.48 < 4.5) print(background.isContrasting(with: text, inContext: .standardLargeText)) // true (4.48 > 3.0) print(background.isContrasting(with: darkText, inContext: .standard)) // true (7.07 > 4.5) print(background.isContrasting(with: darkText, inContext: .enhanced)) // true (7.07 > 7.0) ``` ``` -------------------------------- ### Generating a Color Palette with Gradients Source: https://context7.com/yannickl/dynamiccolor/llms.txt Generates a color palette from a list of colors using a gradient, specifying the number of colors and the color space for interpolation. The .lab color space is recommended for perceptually uniform gradients. ```swift // Apply in gradient palette generation let palette = [red, blue].gradient.colorPalette(amount: 5, inColorSpace: .lab) // palette[0] = red, palette[2] = perceptually-midpoint, palette[4] = blue ``` -------------------------------- ### Extracting HSB Components Source: https://context7.com/yannickl/dynamiccolor/llms.txt Returns Hue (0.0–1.0), Saturation (0.0–1.0), and Brightness (0.0–1.0) components as a named tuple, plus individual properties on iOS/tvOS/watchOS. ```APIDOC ## Extracting HSB Components Returns Hue (0.0–1.0), Saturation (0.0–1.0), and Brightness (0.0–1.0) components as a named tuple, plus individual properties on iOS/tvOS/watchOS. ```swift let color = DynamicColor(hexString: "#3498db") let hsb = color.toHSBComponents() print("H: \(hsb.h), S: \(hsb.s), B: \(hsb.b)") // H: 0.567 (≈204°), S: 0.659, B: 0.859 // Individual properties (iOS/tvOS/watchOS only) let hue = color.hueComponent // 0.567 let saturation = color.saturationComponent // 0.659 let brightness = color.brightnessComponent // 0.859 ``` ``` -------------------------------- ### Generate Cie L*a*b* Color Palette from Array Source: https://github.com/yannickl/dynamiccolor/blob/master/README.md Create a color palette from an array of colors using the `.lab` color space by accessing the `gradient` extension property and calling `colorPalette`. ```swift let labPalette = [blue, red, yellow].gradient.colorPalette(amount: 8, inColorSpace: .lab) ``` -------------------------------- ### Initialize DynamicColor from Hex String or Integer Source: https://context7.com/yannickl/dynamiccolor/llms.txt Create colors from hex strings (with or without '#') or hex integer literals. Supports RGBA hex strings and integers with explicit alpha. Can be used for both UIKit/AppKit and SwiftUI colors. ```swift import DynamicColor // From a hex string (with or without "#") let blue = DynamicColor(hexString: "#3498db") let red = DynamicColor(hexString: "#c0392b") // From an 8-character RGBA hex string (includes alpha) let semiTransparentRed = DynamicColor(hexString: "#FF0934CC") // From a hex integer let green = DynamicColor(hex: 0x2ecc71) // With explicit alpha channel in hex integer let fadedBlue = DynamicColor(hex: 0x3498dbCC, useAlpha: true) // Platform-independent usage (UIColor on iOS/tvOS/watchOS, NSColor on macOS) let color: DynamicColor = DynamicColor(hex: 0x3498db) // SwiftUI (iOS 13+, macOS 10.15+) import SwiftUI let swiftUIColor = Color(hexString: "#3498db") let swiftUIColor2 = Color(hex: 0x3498db) let swiftUIWithAlpha = Color(hex: 0x3498dbFF, useOpacity: true) ``` -------------------------------- ### Tint and Shade Colors with DynamicColor Source: https://github.com/yannickl/dynamiccolor/blob/master/README.md Use `tinted()` and `shaded()` methods to adjust the lightness of a color by mixing with white or black respectively. Amounts can be specified between 0 and 1. ```swift let originalColor = DynamicColor(hexString: "#c0392b") let tintedColor = originalColor.tinted() // equivalent to // tintedColor = originalColor.tinted(amount: 0.2) let shadedColor = originalColor.shaded() // equivalent to // shadedColor = originalColor.shaded(amount: 0.2) ``` -------------------------------- ### Contrast Ratio and WCAG Accessibility Checks Source: https://context7.com/yannickl/dynamiccolor/llms.txt Computes the WCAG 2.0 contrast ratio between two colors and verifies whether they meet accessibility standards for different display contexts (e.g., normal text, large text, enhanced contrast). ```swift let background = DynamicColor(hexString: "#ffffff") // white let text = DynamicColor(hexString: "#767676") // medium gray let darkText = DynamicColor(hexString: "#595959") // darker gray // Compute contrast ratio (same result regardless of order) let ratio = background.contrastRatio(with: text) print(ratio) // ~4.48 (just below AA standard of 4.5) // Check WCAG compliance for different contexts // .standard → min 4.5:1 (AA, normal text) // .standardLargeText → min 3.0:1 (AA, large text ≥18pt or bold ≥14pt) // .enhanced → min 7.0:1 (AAA, normal text) // .enhancedLargeText → min 4.5:1 (AAA, large text) print(background.isContrasting(with: text, inContext: .standard)) // false (4.48 < 4.5) print(background.isContrasting(with: text, inContext: .standardLargeText)) // true (4.48 > 3.0) print(background.isContrasting(with: darkText, inContext: .standard)) // true (7.07 > 4.5) print(background.isContrasting(with: darkText, inContext: .enhanced)) // true (7.07 > 7.0) ``` -------------------------------- ### Create SwiftUI Color from Hex Value Source: https://github.com/yannickl/dynamiccolor/blob/master/README.md Initialize SwiftUI Color using hex integer values. This is a convenient way to create colors for SwiftUI views. ```swift let color = Color(hex: 0x3498db) ``` -------------------------------- ### Create Color from Hex String or Value Source: https://github.com/yannickl/dynamiccolor/blob/master/README.md Initialize UIColor or DynamicColor using hex string or integer values. Supports platform-independent DynamicColor typealias. ```swift let color = UIColor(hexString: "#3498db") // equivalent to // color = UIColor(hex: 0x3498db) ``` ```swift let color = DynamicColor(hex: 0x3498db) // On iOS, WatchOS or tvOS, equivalent to // color = UIColor(hex: 0x3498db) // On OSX, equivalent to // color = NSColor(hex: 0x3498db) ``` -------------------------------- ### Extract HSB Components from Color in Swift Source: https://context7.com/yannickl/dynamiccolor/llms.txt Returns Hue, Saturation, and Brightness components as `CGFloat` values in the 0.0–1.0 range. Use `toHSBComponents()` for a named tuple or individual properties like `hueComponent`, `saturationComponent`, and `brightnessComponent` (iOS/tvOS/watchOS only). ```swift let color = DynamicColor(hexString: "#3498db") let hsb = color.toHSBComponents() print("H: \(hsb.h), S: \(hsb.s), B: \(hsb.b)") // H: 0.567 (≈204°), S: 0.659, B: 0.859 // Individual properties (iOS/tvOS/watchOS only) let hue = color.hueComponent // 0.567 let saturation = color.saturationComponent // 0.659 let brightness = color.brightnessComponent // 0.859 ``` -------------------------------- ### Add Contributor to List Source: https://github.com/yannickl/dynamiccolor/blob/master/CONTRIBUTORS.md Follow this format to add a new contributor to the list. Ensure the list remains sorted alphabetically. ```markdown * [Firstname Lastname|Nickname](github_page_url) ``` -------------------------------- ### Darken and Lighten Colors Source: https://github.com/yannickl/dynamiccolor/blob/master/README.md Create new colors by adjusting the lightness of an existing color. Use an amount between 0 and 1 for adjustment. ```swift let originalColor = DynamicColor(hexString: "#c0392b") let lighterColor = originalColor.lighter() // equivalent to // lighterColor = originalColor.lighter(amount: 0.2) let darkerColor = originalColor.darkened() // equivalent to // darkerColor = originalColor.darkened(amount: 0.2) ``` -------------------------------- ### Saturate, Desaturate, and Grayscale Colors in Swift Source: https://context7.com/yannickl/dynamiccolor/llms.txt Modifies the HSL saturation channel by an amount (0.0–1.0, default 0.2). Use `saturated()` to increase and `desaturated()` to decrease saturation. `grayscaled(mode:)` converts to grayscale using one of four perceptual algorithms: `.luminance` (default), `.lightness`, `.average`, or `.value`. ```swift let base = DynamicColor(hexString: "#c0392b") // Increase saturation by 20% let moreSaturated = base.saturated() let fullySaturated = base.saturated(amount: 1.0) // Decrease saturation by 20% let lessSaturated = base.desaturated() let halfDesaturated = base.desaturated(amount: 0.5) // Grayscale using different perceptual models let gray_luminance = base.grayscaled(mode: .luminance) // WCAG luminance formula (default via .lightness mode) let gray_lightness = base.grayscaled(mode: .lightness) // HSL lightness (default) let gray_average = base.grayscaled(mode: .average) // simple RGB average let gray_value = base.grayscaled(mode: .value) // HSV max component // GrayscalingMode options: // .luminance → (0.299*R + 0.587*G + 0.114*B) — perceptual // .lightness → 0.5 * (max(R,G,B) + min(R,G,B)) — HSL // .average → (R + G + B) / 3 — simple // .value → max(R, G, B) — HSV ``` -------------------------------- ### Color Interpolation in Different Color Spaces Source: https://context7.com/yannickl/dynamiccolor/llms.txt Compares the visual midpoint of mixing two colors across RGB, HSL, and CIE L*a*b* color spaces. Use .lab for perceptually uniform gradients. ```swift let red = DynamicColor(hexString: "#e74c3c") let blue = DynamicColor(hexString: "#3498db") // Comparing the visual midpoint across color spaces let rgbMix = red.mixed(withColor: blue, weight: 0.5, inColorSpace: .rgb) // → Tends toward a muddy brown/purple let hslMix = red.mixed(withColor: blue, weight: 0.5, inColorSpace: .hsl) // → Passes through intermediate hues (violet) let labMix = red.mixed(withColor: blue, weight: 0.5, inColorSpace: .lab) // → Perceptually equidistant — looks equally "between" red and blue ``` -------------------------------- ### Generate HSL Color Palette from Gradient Source: https://github.com/yannickl/dynamiccolor/blob/master/README.md Generate a color palette from a gradient using the HSL color space by specifying `.hsl` in the `colorPalette` method. ```swift let hslPalette = gradient.colorPalette(amount: 8, inColorSpace: .hsl) ``` -------------------------------- ### SwiftUI Color Extensions for DynamicColor Source: https://context7.com/yannickl/dynamiccolor/llms.txt Utilize extensions for SwiftUI's Color to initialize directly from hex strings or integers. Convert DynamicColor objects to SwiftUI Color for use in views. Manipulate DynamicColors and then cast them to SwiftUI Color. ```swift import SwiftUI import DynamicColor struct BrandPalette: View { // Create from hex directly on SwiftUI Color let primary = Color(hexString: "#3498db") let secondary = Color(hex: 0xe74c3c) let accent = Color(hex: 0xf1c40f, useOpacity: false) // Bridge from DynamicColor to SwiftUI Color let base = DynamicColor(hexString: "#2ecc71") var body: some View { VStack(spacing: 8) { // Convert DynamicColor manipulation result to SwiftUI Color Rectangle().fill(Color(base.lighter(amount: 0.3))) Rectangle().fill(Color(base)) Rectangle().fill(Color(base.darkened(amount: 0.3))) // Mix two colors and use in SwiftUI let mixed = DynamicColor(hexString: "#3498db") .mixed(withColor: DynamicColor(hexString: "#e74c3c"), weight: 0.5, inColorSpace: .lab) Rectangle().fill(Color(mixed)) } } } ``` -------------------------------- ### Mix Colors with DynamicColor Source: https://github.com/yannickl/dynamiccolor/blob/master/README.md Mix the receiver color with another color using the `mixed(withColor:weight:inColorSpace:)` method. The `weight` parameter (0 to 1) controls the blend, and `inColorSpace` specifies the color model for mixing. ```swift let originalColor = DynamicColor(hexString: "#c0392b") let mixedColor = originalColor.mixed(withColor: .blue) // equivalent to // mixedColor = originalColor.mixed(withColor: .blue, weight: 0.5) // or // mixedColor = originalColor.mixed(withColor: .blue, weight: 0.5, inColorSpace: .rgb) ``` -------------------------------- ### Extracting RGBA Components Source: https://context7.com/yannickl/dynamiccolor/llms.txt Returns the red, green, blue, and alpha channel values as `CGFloat` in the 0.0–1.0 range as a named tuple. ```APIDOC ## Extracting RGBA Components Returns the red, green, blue, and alpha channel values as `CGFloat` in the 0.0–1.0 range as a named tuple. ```swift let color = DynamicColor(hexString: "#e74c3c") // All components at once let (r, g, b, a) = color.toRGBAComponents() print("R: \(r), G: \(g), B: \(b), A: \(a)") // R: 0.906, G: 0.298, B: 0.235, A: 1.0 // Individual component properties (iOS/tvOS/watchOS only) let red = color.redComponent // 0.906 let green = color.greenComponent // 0.298 let blue = color.blueComponent // 0.235 let alpha = color.alphaComponent // 1.0 // Adjust alpha independently let semiTransparent = color.adjustedAlpha(amount: -0.5) // Alpha becomes 0.5 (1.0 + (-0.5)); values are clamped to 0.0-1.0 ``` ``` -------------------------------- ### Tinting and Shading Colors Source: https://context7.com/yannickl/dynamiccolor/llms.txt Mixes the color with white (tint) or black (shade) by a given weight (0.0–1.0). Default amount is 0.2. Useful for creating UI theme scales. ```swift let base = DynamicColor(hexString: "#c0392b") // Tint: mix with white (increases lightness) let tinted = base.tinted() // 20% white let lightlyTinted = base.tinted(amount: 0.1) // 10% white let heavilyTinted = base.tinted(amount: 0.7) // 70% white → pastel // Shade: mix with black (decreases lightness) let shaded = base.shaded() // 20% black let lightlyShaded = base.shaded(amount: 0.1) // 10% black let deepShaded = base.shaded(amount: 0.6) // 60% black → very dark // Generate a 5-step tint/shade scale for a UI theme let tints = [0.1, 0.2, 0.4, 0.6, 0.8].map { base.tinted(amount: CGFloat($0)) } let shades = [0.1, 0.2, 0.4, 0.6, 0.8].map { base.shaded(amount: CGFloat($0)) } ``` -------------------------------- ### Convert Colors to Hex Representations in Swift Source: https://context7.com/yannickl/dynamiccolor/llms.txt Extracts hex string or integer representations from a color. Use `toHexString()` for string output, `toHex()` for RGB integer, `toRGBA()` for packed RGBA integer, and `toAGBR()` for packed AGBR integer. Comparison helpers like `isEqual(toHexString:)` and `isEqual(toHex:)` are also available. ```swift let color = DynamicColor(hexString: "#3498db") // Hex string output: "#3498db" let hexString = color.toHexString() print(hexString) // "#3498db" // Hex integer output (RGB only, no alpha) let hexInt: UInt32 = color.toHex() print(String(format: "0x%06x", hexInt)) // "0x3498db" // RGBA packed integer (R<<24 | G<<16 | B<<8 | A) let rgba: UInt32 = color.toRGBA() // AGBR packed integer (for some graphics APIs) let agbr: UInt32 = color.toAGBR() // Comparison helpers let isSame = color.isEqual(toHexString: "#3498db") // true let isSame2 = color.isEqual(toHex: 0x3498db) // true ``` -------------------------------- ### Extract RGBA Components from Color in Swift Source: https://context7.com/yannickl/dynamiccolor/llms.txt Returns RGBA components as `CGFloat` values in the 0.0–1.0 range. Use `toRGBAComponents()` for a named tuple or individual properties like `redComponent`, `greenComponent`, `blueComponent`, and `alphaComponent` (iOS/tvOS/watchOS only). `adjustedAlpha(amount:)` can modify the alpha channel. ```swift let color = DynamicColor(hexString: "#e74c3c") // All components at once let (r, g, b, a) = color.toRGBAComponents() print("R: \(r), G: \(g), B: \(b), A: \(a)") // R: 0.906, G: 0.298, B: 0.235, A: 1.0 // Individual component properties (iOS/tvOS/watchOS only) let red = color.redComponent // 0.906 let green = color.greenComponent // 0.298 let blue = color.blueComponent // 0.235 let alpha = color.alphaComponent // 1.0 // Adjust alpha independently let semiTransparent = color.adjustedAlpha(amount: -0.5) // Alpha becomes 0.5 (1.0 + (-0.5)); values are clamped to 0.0-1.0 ``` -------------------------------- ### Generate RGB Color Palette from Gradient Source: https://github.com/yannickl/dynamiccolor/blob/master/README.md Use the `colorPalette(amount:inColorSpace:)` method on a `DynamicGradient` object to generate a specified number of colors. The default color space is RGB. ```swift let rgbPalette = gradient.colorPalette(amount: 8) ``` -------------------------------- ### Querying Color Lightness and Luminance Source: https://context7.com/yannickl/dynamiccolor/llms.txt Determines whether a color is perceptually light or dark using a simple threshold, and computes its WCAG 2.0 relative luminance value. Useful for choosing appropriate text colors for backgrounds. ```swift let white = DynamicColor.white let black = DynamicColor.black let yellow = DynamicColor(hexString: "#f1c40f") let navy = DynamicColor(hexString: "#2c3e50") // Simple light/dark classification (threshold at 0.5 brightness) print(white.isLight()) // true print(black.isLight()) // false print(yellow.isLight()) // true print(navy.isLight()) // false // WCAG 2.0 relative luminance (0.0 = darkest, 1.0 = lightest) print(white.luminance) // ~1.0 print(black.luminance) // ~0.0 print(yellow.luminance) // ~0.929 // Choose text color based on background func textColor(for background: DynamicColor) -> DynamicColor { return background.isLight() ? .black : .white } let label = textColor(for: navy) // → .white ``` -------------------------------- ### Tinting and Shading Source: https://context7.com/yannickl/dynamiccolor/llms.txt Mixes the color with white (tint) or black (shade) by a given weight (0.0–1.0, default 0.2), following traditional artist color theory. ```APIDOC ## Tinting and Shading Mixes the color with white (tint) or black (shade) by a given weight (0.0–1.0, default 0.2), following traditional artist color theory. ```swift let base = DynamicColor(hexString: "#c0392b") // Tint: mix with white (increases lightness) let tinted = base.tinted() // 20% white let lightlyTinted = base.tinted(amount: 0.1) // 10% white let heavilyTinted = base.tinted(amount: 0.7) // 70% white → pastel // Shade: mix with black (decreases lightness) let shaded = base.shaded() // 20% black let lightlyShaded = base.shaded(amount: 0.1) // 10% black let deepShaded = base.shaded(amount: 0.6) // 60% black → very dark // Generate a 5-step tint/shade scale for a UI theme let tints = [0.1, 0.2, 0.4, 0.6, 0.8].map { base.tinted(amount: CGFloat($0)) } let shades = [0.1, 0.2, 0.4, 0.6, 0.8].map { base.shaded(amount: CGFloat($0)) } ``` ``` -------------------------------- ### Adjust Color Saturation and Grayscale Source: https://github.com/yannickl/dynamiccolor/blob/master/README.md Modify the saturation of a color or convert it to grayscale using different modes. Use an amount between 0 and 1 for saturation adjustments. ```swift let originalColor = DynamicColor(hexString: "#c0392b") let saturatedColor = originalColor.saturated() // equivalent to // saturatedColor = originalColor.saturated(amount: 0.2) let desaturatedColor = originalColor.desaturated() // equivalent to // desaturatedColor = originalColor.desaturated(amount: 0.2) // equivalent to // let grayscaledColor = originalColor.grayscaled(mode: .luminance) 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) ``` -------------------------------- ### Querying Color Lightness and Luminance Source: https://context7.com/yannickl/dynamiccolor/llms.txt Determines whether a color is perceptually light or dark, and computes its WCAG 2.0 relative luminance value. ```APIDOC ## Querying Color Lightness and Luminance Determines whether a color is perceptually light or dark, and computes its WCAG 2.0 relative luminance value. ```swift let white = DynamicColor.white let black = DynamicColor.black let yellow = DynamicColor(hexString: "#f1c40f") let navy = DynamicColor(hexString: "#2c3e50") // Simple light/dark classification (threshold at 0.5 brightness) print(white.isLight()) // true print(black.isLight()) // false print(yellow.isLight()) // true print(navy.isLight()) // false // WCAG 2.0 relative luminance (0.0 = darkest, 1.0 = lightest) print(white.luminance) // ~1.0 print(black.luminance) // ~0.0 print(yellow.luminance) // ~0.929 // Choose text color based on background func textColor(for background: DynamicColor) -> DynamicColor { return background.isLight() ? .black : .white } let label = textColor(for: navy) // → .white ``` ``` -------------------------------- ### Mixing Colors Source: https://context7.com/yannickl/dynamiccolor/llms.txt Blends two colors by interpolating their components at a given weight (0.0–1.0) in a chosen color space (RGB, HSL, HSB, or L*a*b*). Different color spaces produce visually distinct blend results. ```APIDOC ## Mixing Colors Blends two colors by interpolating their components at a given weight (0.0–1.0) in a chosen color space (RGB, HSL, HSB, or L*a*b*). Different color spaces produce visually distinct blend results. ```swift let red = DynamicColor(hexString: "#e74c3c") let blue = DynamicColor(hexString: "#3498db") // Equal mix in RGB (default) let purpleRGB = red.mixed(withColor: blue) // equivalent to: red.mixed(withColor: blue, weight: 0.5, inColorSpace: .rgb) // Bias toward blue (75% blue, 25% red) let mostlyBlue = red.mixed(withColor: blue, weight: 0.75) // Mix in different color spaces — each produces a different result let mixedHSL = red.mixed(withColor: blue, weight: 0.5, inColorSpace: .hsl) let mixedHSB = red.mixed(withColor: blue, weight: 0.5, inColorSpace: .hsb) let mixedLab = red.mixed(withColor: blue, weight: 0.5, inColorSpace: .lab) // Lab mixing is perceptually uniform — the midpoint looks equally "between" both // Tint (mix with white) and Shade (mix with black) are built on top of mixed() let tint = red.tinted(amount: 0.3) // mixed(withColor: .white, weight: 0.3) let shade = red.shaded(amount: 0.3) // mixed(withColor: .black, weight: 0.3) ``` ``` -------------------------------- ### Invert Color with DynamicColor Source: https://github.com/yannickl/dynamiccolor/blob/master/README.md The `inverted()` method inverts the red, green, and blue components of a color while preserving its opacity. ```swift let originalColor = DynamicColor(hexString: "#c0392b") let invertedColor = originalColor.inverted() ``` -------------------------------- ### Converting Colors to Hex Representations Source: https://context7.com/yannickl/dynamiccolor/llms.txt Extracts the hex string or integer representation from a color, with variants for RGB-only or with alpha channel. ```APIDOC ## Converting Colors to Hex Representations Extracts the hex string or integer representation from a color, with variants for RGB-only or with alpha channel. ```swift let color = DynamicColor(hexString: "#3498db") // Hex string output: "#3498db" let hexString = color.toHexString() print(hexString) // "#3498db" // Hex integer output (RGB only, no alpha) let hexInt: UInt32 = color.toHex() print(String(format: "0x%06x", hexInt)) // "0x3498db" // RGBA packed integer (R<<24 | G<<16 | B<<8 | A) let rgba: UInt32 = color.toRGBA() // AGBR packed integer (for some graphics APIs) let agbr: UInt32 = color.toAGBR() // Comparison helpers let isSame = color.isEqual(toHexString: "#3498db") // true let isSame2 = color.isEqual(toHex: 0x3498db) // true ``` ```