Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Swift gradient output #1

Open
larsenwork opened this issue May 25, 2018 · 1 comment
Open

Add Swift gradient output #1

larsenwork opened this issue May 25, 2018 · 1 comment
Labels
enhancement New feature or request good first issue Good for newcomers

Comments

@larsenwork
Copy link
Owner

Similar to copy CSS - should be easy: https://stackoverflow.com/a/33800041

@larsenwork larsenwork added enhancement New feature or request good first issue Good for newcomers labels May 25, 2018
@hanawat
Copy link

hanawat commented May 15, 2021

Thanks for the great plugin.
This is snippets for use with Swift from the output CSS.

/* Gradient direction goes here */
hsl(272,29.64%,49.61%) 0%,
hsl(271.41,29.91%,49.28%) 0.05%,
hsl(269.78,30.69%,48.37%) 0.36%,
...
let layer = CAGradientLayer()
layer.colors = [
    .init(hue: 272.00, saturation: 0.2964, lightness: 0.4961),
    .init(hue: 271.41, saturation: 0.2991, lightness: 0.4928),
    .init(hue: 269.78, saturation: 0.3069, lightness: 0.4837),
    ...
]
layer.locations = [
    0.0000,
    0.0005,
    0.0036,
    ...
]
extension UIColor {
    convenience init(hue: CGFloat, saturation: CGFloat, lightness: CGFloat, alpha: CGFloat = 1.0) {
        precondition(.zero...360.0 ~= hue, "Input hue is out of range 0 to 360")
        precondition(.zero...1.0 ~= saturation, "Input saturation is out of range 0 to 1")
        precondition(.zero...1.0 ~= lightness, "Input lightness is out of range 0 to 1")
        precondition(.zero...1.0 ~= alpha, "Input alpha is out of range 0 to 1")
        let brightness = lightness + saturation * min(lightness, 1.0 - lightness)
        let saturation = brightness.isZero ? .zero : 2.0 * (1.0 - lightness / brightness)
        self.init(hue: hue / 360.0, saturation: saturation, brightness: brightness, alpha: alpha)
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

2 participants