Skip to content

Convert Colors to Hex strings and vice versa, compare colors,... in a single line of code.

Notifications You must be signed in to change notification settings

NVMNovem/NVMColor_swift

Repository files navigation

NVMColor_header

iOS · macOS · watchOS · tvOS


A pure Swift library that allows you to easily convert SwiftUI Colors to Hex String and vice versa.

This framework extends the Color, UIColor and NSColor types. Depending on the target version, you have the same functions as SwifUI's Color.

This project is created and maintained by Novem.


Installation

You can use The Swift Package Manager (SPM) to install NVMRegion by adding the following description to your Package.swift file:

import PackageDescription

let package = Package(
    name: "YOUR_PROJECT_NAME",
    dependencies: [
        .package(url: "https://github.com/NVMNovem/NVMColor_swift", from: "1.0.0"),
    ]
)

Then run swift build.

You can also install using SPM in your Xcode project by going to "Project->NameOfYourProject->Swift Packages" and placing "https://github.com/NVMNovem/NVMColor" in the search field. Then select the option that is most suited for your needs.

Usage Guide

Hex string to Color

let color = Color(hex: "159F84")

Color to Hex string

let hexColor = Color.green.hex

Compare 2 Colors

Non valid comparisson

if Color.green.isEqual(to: .red) {
   //Will not be executed
}

Non valid comparisson using tolerance

if Color.green.isEqual(to: .red, tolerance: 0.5) {
   //Will not be executed
}

Valid comparisson

if Color.green.isEqual(to: Color(hex: "34C759")) {
   //Will be executed
}

Valid comparisson using tolerance

if Color.green.isEqual(to: Color(hex: "34C759"), tolerance: 0.5) {
   //Will be executed
}

You can also compare by using the NVMColor function.

if NVMColor.colorsAreEqual(.green, to: Color(hex: "34C759")) {
   //Will be executed
}

Themed Color

You can get a color that fits the current device sheme if the input color is to close to the device scheme color.

struct MyView: View {
    @Environment(\.colorScheme) private var colorScheme
    
    var body: some View {
        Text("My custom text")
            .foregroundColor(Color(hex: "2E3240")!.themedColor(colorScheme))
    }
}

About

Convert Colors to Hex strings and vice versa, compare colors,... in a single line of code.

Topics

Resources

Stars

Watchers

Forks

Languages