yannickl / dynamiccolor Goto Github PK
View Code? Open in Web Editor NEWYet another extension to manipulate colors easily in Swift and SwiftUI
Home Page: http://cocoadocs.org/docsets/DynamicColor
License: MIT License
Yet another extension to manipulate colors easily in Swift and SwiftUI
Home Page: http://cocoadocs.org/docsets/DynamicColor
License: MIT License
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.
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:
Get the relative luminosity value of a color following the WCAG specs: https://www.w3.org/TR/WCAG20/#relativeluminancedef
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.
Update for Swift 4 please
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)
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 ?
It would be nice to select the scale of each color in a gradient to gain in control.
Here an API example:
let gradient = DynamicGradient(colors: [blue, red, yellow], scale: [0,0.25,1])
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. 🤦♂️
For the moment we can't publish the last pod due to this Cocoapods issue: CocoaPods/CocoaPods#5805.
It seems we also need to wait for CocoaPods/CocoaPods#5650 resolution.
After installing the v3.1.0 in my project with Xcode 3.0 it returns an error that some code is not swift 3.0. I looked over the example and it's a different code from the pod one.
Any suggestion?
The clip parameter labels maximum and minimum are required in Xcode 7 beta 6 which causes compilation errors
Hello,
Is that planned to update this lib to swift 5 ?
Thanks you
Building archive fails. Modification must be done to the project settings:
Carthage/Carthage#535
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?
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.
How to get Swift2.3 version?
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.
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
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)
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.
Is your feature request related to a problem? Please describe.
No
Describe the solution you'd like
Support for:
Describe alternatives you've considered
Additional context
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
}
}
first
carthage update --platform iOS
with .. in cartfile
github "yannickl/DynamicColor" >= 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:
github "yannickl/DynamicColor" >= 5.0.01
in Cartfilecarthage bootstrap/update
Expected behavior
A clear and concise description of what you expected to happen.
Smartphone (please complete the following information):
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 🤷♂
When use a RGBA hex String like "0xffffffff",will get an int overflow bug in iOS 9.
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
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:
HSL
color space, increasing the steps the gradient seems to make the opposite turn of the wheel instead of the shortest way.HSB
I cannot understand which way is used.Maybe this func
could have a bug: mixedHue(source: CGFloat, target: CGFloat)
in DynamicColor+Mixing.swift
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)
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
Using carthage, it builds DynamicColor/Examples/DynamicColorExample.xcodeproj
for iOS only. It seems like its missing a proper xcodeproj file.
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.
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?
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
Could this be re-compiled in Xcode 8.3 for Swift 3.1?
https://github.com/yannickl/DynamicColor/releases is missing changelog information.
For ex : https://github.com/Alamofire/Alamofire/releases
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:
~/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.)
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:
Expected behavior
To not crash. :)
Smartphone (please complete the following information):
Additional context
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.