swift-css icon indicating copy to clipboard operation
swift-css copied to clipboard

Gradient stop percentage

Open Amzd opened this issue 5 years ago • 2 comments

A gradients stops can have a percentage to specify where the color starts.

For example, these are the same:

linear-gradient(red, orange, yellow, green, blue); 
linear-gradient(red 0%, orange 25%, yellow 50%, green 75%, blue 100%);

Currently the gradient cases on Color only receive [Color] as stops.

I think there should be a Color case that looks something like:

case linearGradient(_ direction: LinearGradient.Direction, _ stops: [(Color, Int)])

or:

case linearGradient(_ direction: LinearGradient.Direction, _ stops: [ColorStop])

// ...

public struct ColorStop: CustomStringConvertible {
    let color: Color
    let positions: [Int]

    public var description: String {
        color.description + positions.map { String($0) + "%" } .joined(separator: " ")
    }

    public static func color(_ color: Color, at percentages: Int...) -> ColorStop {
        return ColorStop(color: color, positions: percentages)
    }
}

Amzd avatar Dec 16 '19 14:12 Amzd

Link to article about stop percentages: https://developer.mozilla.org/en-US/docs/Web/CSS/linear-gradient#Customizing_Gradients

Amzd avatar Dec 16 '19 14:12 Amzd

Thanks for this! I like the first one better. If you make a PR, I’ll merge it.

carson-katri avatar Dec 16 '19 16:12 carson-katri