GithubHelp home page GithubHelp logo

yannickl / dynamiccolor Goto Github PK

View Code? Open in Web Editor NEW
3.0K 56.0 140.0 483 KB

Yet another extension to manipulate colors easily in Swift and SwiftUI

Home Page: http://cocoadocs.org/docsets/DynamicColor

License: MIT License

Swift 99.12% Ruby 0.88%
uicolor nscolor gradient color hex-strings hex swiftui

dynamiccolor's People

Contributors

basthomas avatar duemunk avatar hartbit avatar jlnquere avatar landtanin avatar naoyashiga avatar ps-ferreira avatar regexident avatar richardgroves avatar yannickl avatar zld 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  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

dynamiccolor's Issues

No such module 'DynamicColor'

Describe the bug
Suddenly I get error "No such module 'DynamicColor'" when building for macOS (Catalyst). Xcode 12.0.1.

To Reproduce
Build for macOS Catalyst.

Expected behavior
Should build for macOS Catalyst.

Additional context
I didn'y have this issue before, I have no idea. All I did I added new Target for AppClip (but AppClip is only for iOS)

Shape Style?

Hi,

I've got this code:

VStack {
						Text("Active Window Border")
						ColorPickerRing(color: $activeWindowBorderColor, strokeWidth: 30)
							.frame(width: 100, height: 100, alignment: .center)
						HStack {
							Button("Cancel") {
								// somehow reset color
								self.colorPicker = false
								
							}
							Button("OK") {
								self.colorPicker = false
							}
							RoundedRectangle(cornerRadius: 5.0)
								.fill(activeWindowBorderColor)
						}
					}.frame(width: 350, height: 350, alignment: .center)

That throws: Instance method 'fill(_:style:)' requires that 'DynamicColor' (aka 'NSColor') conform to 'ShapeStyle'

But when I user Color.white it works.

Can anybody explain that thing with ShapeStyle?

toRGBA()

extension DynamicColor {
    func toRGBA() -> UInt32 {
        func roundToHex(_ x: CGFloat) -> UInt32 {
            guard x > 0 else { return 0 }
            let rounded: CGFloat = round(x * 255)
            return UInt32(rounded)
        }
        let rgba = toRGBAComponents()
        return roundToHex(rgba.a) << 24 | roundToHex(rgba.b) << 16 | roundToHex(rgba.g) << 8 | roundToHex(rgba.r)
    }
}

I use this method when working low level with images raw data buffers, maybe could be added ?

Alpha value

Hi, I tried to install the alpha value through this library, but the results did not get, tell me how to fix it?

My example
UIColor.white.adjustedAlpha(amount: 0.75)

Minor issue: proposed fix for improved Swift compilation times

File: DynamicColor.swift

Note: this issue is not a developer fault: it's just a limitation of the Swift compiler at this stage. Using Xcode 9.2 beta 1 (9C32c)

Problem:

In the nested function roundToHex(_ x: CGFloat), the following line takes over 200ms to compile on a MBP:

return UInt32(round(1000 * x) / 1000 * 255)

Proposed Fix

Adding the type saves Xcode the job of thinking:

let rounded: CGFloat = round(1000 * x)
return UInt32(rounded / 1000 * 255)

Cheers, and thanks for all your work on this project.

Make precendence of basic math operators more explicit.

This caused me some grief with recent update to Xcode 11 GM. Somehow, Swift (5.1) started re-ordering some of DynamicColor's math expressions to use + and - overloads I had created to handle mixing CGFloat and Int without the need to cast. This was possible because DynamicColor code and mine are in the same module. Strangely, this did not occur before Swift 5.1

The change in Swift's behavior caused major breakage in my app colors, because the math expressions became incorrect -- for example, let r = hueToRGB(m1: m1, m2: m2, h: h + 1 / 3) would compute h + 1 before / 3.

Notwithstanding the dumbness of my operator overloads (!), I think DynamicColor should make these expressions more explicit to shield future problems from occurring.

Using that same example, change:
let r = hueToRGB(m1: m1, m2: m2, h: h + 1 / 3)
to
let r = hueToRGB(m1: m1, m2: m2, h: h + (1.0 / 3.0))

And of course, other places where similar precedence problems may occur.

I would be happy to create the pull request for this as soon as I have a moment. Else, if someone else does I'd be happy to review it.

Cautionary Tale: hue ranges have changed

This is more of a lighthouse for anyone who crashes on the rocks of adjustedHue as I did!

The range has been changed from [0, 1] to [-360, 360]. So if you don't see any changes when shift your color's hue by 0.1, you'll know why; the color has changed, but by very little!

Oh well, next time I'll read the Change Log a bit more carefully. :p

Awesome project, BTW. Thanks a bunch for all your work.

Implementing SwiftLint

Hello,

I think that could be a great thing to add SwiftLint to this project. It will force us to maintain clean code. It also helps to avoid some mistakes.

And there is a little trick: if we use ’swift lint autocorrect’, it fixes automatically the majority of the code formatting issues! What do you think about it?

If it’s OK to you, I’ll make a PR with the SwiftLint implantation.

Thoughts on making hexString init failable?

Would you accept a PR to make the hexString init failable, so it'd return nil rather than a black color?

Something like:

  public convenience init?(hexString: String) {
    let hexString = hexString.trimmingCharacters(in: .whitespacesAndNewlines)
    let scanner   = Scanner(string: hexString)

    if hexString.hasPrefix("#") {
      scanner.scanLocation = 1
    }

    var color: UInt32 = 0

    if scanner.scanHexInt32(&color) {
      self.init(hex: color, useAlpha: hexString.count > 7)
    }
    else {
      return nil
    }
  }

Broken release builds – misconfigured SwiftUI dependency

Describe the bug
Since v5.0.0 DynamicColor.framework does not build in release, so breaking release build of host apps. Reproducible in your own demo app.

To Reproduce
Steps to reproduce the behavior:

  1. Open DynamicColorExample.xcodeproj in Xcode 11.3
  2. Select target iOSExample + Generic iOS Device
  3. Build
  4. Build of DynamicColor.framework fails with error:
~/Dev/External/Graphics/DynamicColor/Sources/SwiftUIColor.swift:30:18: error: use of undeclared type 'Color'
public extension Color {

Expected behavior
build does not fail

Smartphone (please complete the following information):
any

Additional context
Suspect this is because the framework is not declaring correct dependency on SwiftUI.framework - needs to be added as optional linked framework.
This is problematic for projects that do not want to introduce dependency on SwiftUI.
Would also help if you could split your podspec into a basic that excludes SwiftUIColor.swift and a subspec that adds it for those who need it; still leaves Carthage users in a bind. Alternatively, duplicate your targets to provide with & without SwiftUI versions of the frameworks. (No plans here to adopt SwiftUI until its well past version 2.)

iOS Privacy Policy

Apple says "Starting May 1: You'll need to include approved reasons for the listed APIs used by your app's code to upload a new or updated app to App Store Connect."

Please add privacy policy otherwise we couldn't use this library.

Fatal error: Not enough bits to represent the passed value

Describe the bug
We got a crash on an iPhone 5C running iOS 10.3.3 and it's triggered by this line:

UIColor(hex: 0xffffff80, useAlpha: true)

The crash happens on line 87 in DynamicColor.swift since Int is the same size as Int32 on 32 bit platforms.

To Reproduce
Steps to reproduce the behavior:

  1. Add the line above to a project
  2. Run it on an iPhone 5 using iOS 10.x in a simulator
  3. See crash

Expected behavior
To not crash. :)

Smartphone (please complete the following information):

  • Device: 32 bit devices, i.e. iPhone 5C
  • OS: 10.3

Additional context

Incorrect gradients

Trying to make a simple gradient between red #ff0000 and blu #0000ff with color spaces HSL and HSB I have an unexpected result. It seem works correctly with RGB and L*a*b*.

Expected behavior should be similar to RGB and L*a*b* spaces.

Additional considerations:

  • For HSL color space, increasing the steps the gradient seems to make the opposite turn of the wheel instead of the shortest way.
  • For HSB I cannot understand which way is used.

Maybe this func could have a bug: mixedHue(source: CGFloat, target: CGFloat) in DynamicColor+Mixing.swift

hsl

hsb

lab

rgb

Int overflows bug

When use a RGBA hex String like "0xffffffff",will get an int overflow bug in iOS 9.

Inverting brightness only.

It would be helpful to invert only the brightness while retaining the hue and saturation of a color. Is this possible? There is currently DynamicColor.invert. Would it be useful to add a DynamicColor.invertBrightness ?

I tried with the following code, and I get inconsistent results (for example, light-medium orange becomes almost black instead of dark orange). Mostly I'm finding the resulting colors are darker then I would expect.

let hue = hueComponent
let saturation = saturationComponent
let brightness = 1 - brightnessComponent
let alpha = alphaComponent
return DynamicColor(hue:hue, saturation:saturation, brightness:brightness, alpha:alpha)

Can not build in simulator on mac m1 device

It is working fine on device but when i run for simulator on my m1 device it gives me this error.
Could not find module 'DynamicColor' for target 'x86_64-apple-ios-simulator'; found: arm64, arm64-apple-ios-simulator

Manipulate alpha component of color

How about a convenience function for manipulating the alpha component of a color? It seems like you can manipulate just about every other aspect of the color, except that.

If there's already an easy way to do this that I have missed?

DynamicColor.lighter inconsistent results.

In testing DynamicColor I noticed that "lighter" is providing inconsistent results. Results seem almost random, but certainly unreliable. Sometimes, there appears to be no lightening at all even though the original color is quite dark.
EDIT: Never mind. It was my test code. 🤦‍♂️

Failed to verify bitcode issue

I get the following error message while trying to do an Ad-Hoc export of project that uses DynamicColor framework:

Failed to verify bitcode in DynamicColor.framework/DynamicColor:

description = "Failed to verify bitcode in DynamicColor.framework/DynamicColor:\nerror: Cannot extract bundle
level = ERROR;
type = "malformed-payload";

It happens in Compile Bitcode expert step.

Gradient colors

It would be nice to have a method that generates color on a gradient between two colors.
API might look like that:

red.gradientTo(green).colorAt(0.5) // yellow-ish

little enhancement

You can add the

    public final func readableColor() -> DynamicColor {
        return isLightColor() ? UIColor.blackColor() : UIColor.whiteColor()
    }

and or another that use the inverse / complement at your discretion
it's not much related to the colors elaboration it's more an utility that can be useful

Version 5.0 was not found using pod

Describe the bug
I updated repo to the latest and found that the latest version of DynamicColor was 4.2.0, but the latest version 5.0 could not be found

To Reproduce
Steps to reproduce the behavior:

  1. open the 'terminal'
  2. input 'pod repo update'
  3. and input 'pod search DynamicColor'
  4. See the latest version was 4.2.0

hex should support the alpha channel

Thanks for the library! I saw that currently alpha is always set to FF, which limits its usefulness. Could you update so that alpha value could also be supported? e.g. when the hex has 8 digits, use the last 2 as alpha, if the hex has 6 digits, default to FF

Carthage build fails for version 5.0.0

I've been unsuccessfully trying to add this project as a Carthage dependency.
In the console I get:

*** Building scheme "DynamicColor" in DynamicColorExample.xcodeproj
Build Failed
	Task failed with exit code 65:
   ...

And the file clearly prints out the error:

/Users/mario/Projects/tests/DCTest/Carthage/Checkouts/DynamicColor/Sources/SwiftUIColor.swift:30:18: error: use of undeclared type 'Color'
public extension Color {
                 ^~~~~

** ARCHIVE FAILED **


The following build commands failed:
	CompileSwift normal arm64
	CompileSwiftSources normal armv7 com.apple.xcode.tools.swift.compiler
	CompileSwift normal armv7
(3 failures)

To Reproduce
Steps to reproduce the behavior:

  1. Add github "yannickl/DynamicColor" >= 5.0.01 in Cartfile
  2. Run carthage bootstrap/update
  3. See error

Expected behavior
A clear and concise description of what you expected to happen.

Smartphone (please complete the following information):

  • Carthage version: 0.34.0
  • MacOS: tried on Mojave and Catalina

Additional context
I did try to debug the error, but I failed. The line in question is using the struct from SwiftUI, and it's enclosed with @available attribute, but still it somehow fails 🤷‍♂

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.