Giter Club home page Giter Club logo

darkmode's Introduction

mit platform Lang SPM compatible Tests codebeat badge

DarkMode

🔦 DarkMode made simple

Description

  • DarkMode is a Swift library that simplifies the process of implementing dark mode in iOS and macOS applications.
  • With this library, developers can easily switch between light and dark modes, and organize their themes and colors in a clean and efficient way.
  • The library provides a set of tools and utilities that make it easy to manage the appearance of an application, and it is compatible with Swift Package Manager (SPM), making it easy to integrate into any project.
  • Whether you're building a new app or updating an existing one, DarkMode can help you create a seamless and intuitive user experience across all devices and platforms.

Table of content:

Installation

To install DarkMode, use Swift Package Manager (SPM) and add the following to your Package.swift file: .package(url: "https://github.com/sentryco/Darkmode.git", .branch("master"))

Example (iOS)

struct Theme {
   struct Text {
      static let title: Color = .init(light: .green, dark: .blue)
   }
   struct Background {
      static let primary: Color = .init(light: .red, dark: .yellow)
   }
}
class View: UIView {
   override init(frame: CGRect) {
      super.init(frame: frame)
      self.backgroundColor = Theme.Background.primary
      // Programatically change dark / light mode (this can also be done in prefs)
      // UIApplication.shared.override(.dark) // changes colors to dark-mode
      // UIApplication.shared.override(.light) // changes colors to light-mode
   }
}

Example (macOS)

class View: NSView {
   override public init(frame: CGRect) {
      super.init(frame: frame)
      self.wantsLayer = true // if true then view is layer backed
      self.layer?.backgroundColor = Theme.Background.primary.cgColor // since cgColor is not dynamic, place this code in the draw, or layout method to make it react to OS appearance change
   }
}

Nice way to organize a theme that has support for dark and light mode:

/**
 * App-color-scheme
 * ## Examples:
 * Theme.Text.header // white
 * Theme.Background.secondary // lightGray
 */
internal class Theme {
   struct Text {
      static let title: Color = .init(light: #colorLiteral(red: 0.2605174184, green: 0.2605243921, blue: 0.260520637, alpha: 1), dark: #colorLiteral(red: 0.8039215803, green: 0.8039215803, blue: 0.8039215803, alpha: 1))
      static let header: Color = .init(light: .x6A6A6A, dark: .init(hex: 0xFEFEFE))
   }
   struct Button {
      static let accessory: Color = .init(light: #colorLiteral(red: 0.1764705926, green: 0.4980392158, blue: 0.7568627596, alpha: 1), dark: #colorLiteral(red: 0.1019607857, green: 0.2784313858, blue: 0.400000006, alpha: 1))
   }
   struct Background {
      static let primary: Color = .init(light: #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0), dark: #colorLiteral(red: 0.08450166136, green: 0.08400709182, blue: 0.08488682657, alpha: 1))
      static let secondary: Color = .init(light: #colorLiteral(red: 0.8039215803, green: 0.8039215803, blue: 0.8039215803, alpha: 1), dark: #colorLiteral(red: 0.1298420429, green: 0.1298461258, blue: 0.1298439503, alpha: 1))
      static let tertiary: Color = .init(light: #colorLiteral(red: 0.6000000238, green: 0.6000000238, blue: 0.6000000238, alpha: 1), dark: #colorLiteral(red: 0.2605174184, green: 0.2605243921, blue: 0.260520637, alpha: 1))
   }
}

Nice way to organize colors:

/**
 * Nice way to organize colors, hex + visual
 * - Note: Only for internal testing
 */
extension Color {
   static let xF9EFB1 = #colorLiteral(red: 0.9764705882352941, green: 0.9372549019607843, blue: 0.6941176470588235, alpha: 1)
   static let x6A6A6A = #colorLiteral(red: 0.4156862745098039, green: 0.4156862745098039, blue: 0.4156862745098039, alpha: 1)
   static let xFEFEFE = #colorLiteral(red: 0.9960784313725490, green: 0.9960784313725490, blue: 0.9960784313725490, alpha: 1)
   static let x202020 = #colorLiteral(red: 0.1254901960784314, green: 0.1254901960784314, blue: 0.1254901960784314, alpha: 1)
}

Resources:

License

This project is licensed under the MIT License. See the LICENSE file for details.

darkmode's People

Contributors

eonist avatar orchidfire avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

orchidfire

darkmode's Issues

Backward compatibility

// ⚠️️ this also works ⚠️️, might be more backward / forward compatible?
// self.init(name: nil) {
//   switch $0.name {
//   case .darkAqua, .vibrantDark, .accessibilityHighContrastDarkAqua, .accessibilityHighContrastVibrantDark:
//      return dark
//   default:
//      return light
//   }
// }

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.