๐Ÿ”ดGradient

import SwiftUI

extension Gradient {
    
    // general linear gradient ---------------------------
    
    /// `Gradient.linear(from: start, to: end, colors: colors)`
    ///
    /// - Parameters:
    ///   - from: ๆผธๅฑค้–‹ๅง‹็š„ๅœฐๆ–น
    ///   - end: ๆผธๅฑค็ตๆŸ็š„ๅœฐๆ–น
    ///   - colors: ๆผธๅฑค้ก่‰ฒ (`[Color]`)
    public static func linear(
        from start: UnitPoint, 
        to     end: UnitPoint, 
        colors    : [Color]       // use array
    ) -> LinearGradient 
    {
        LinearGradient(
            gradient  : Gradient(colors: colors), 
            startPoint: start, 
            endPoint  : end
        )
    }
    
    /// `Gradient.linear(from: start, to: end, colors: c1, c2, c3)`
    ///
    /// - Parameters:
    ///   - from: ๆผธๅฑค้–‹ๅง‹็š„ๅœฐๆ–น
    ///   - end: ๆผธๅฑค็ตๆŸ็š„ๅœฐๆ–น
    ///   - colors: ๆผธๅฑค้ก่‰ฒ (`Color...`)
    public static func linear(
        from start: UnitPoint, 
        to     end: UnitPoint, 
        colors    : Color...     // use variadic parameter
    ) -> LinearGradient 
    {
        linear(from: start, to: end, colors: colors)
    }
    
    // specialized linear gradients ------------------------
    
    /// `Gradient.down(colors)`
    /// - Parameters:
    ///   - colors: ๆผธๅฑค้ก่‰ฒ (`[Color]`)
    public static func down(_ colors: [Color]) -> LinearGradient {
        linear(from: .top, to: .bottom, colors: colors)
    }
    
    /// `Gradient.down(color1, color2, color3)`
    public static func down(_ colors: Color...) -> LinearGradient {
        down(colors)
    }
    
    /// `Gradient.up(colors)`
    public static func up(_ colors: [Color]) -> LinearGradient {
        linear(from: .bottom, to: .top, colors: colors)
    }
    
    /// `Gradient.up(color1, color2, color3)`
    public static func up(_ colors: Color...) -> LinearGradient {
        up(colors)
    }
    
    
    /// `Gradient.right(colors)`
    /// - Parameters:
    ///   - colors: ๆผธๅฑค้ก่‰ฒ (`[Color]`)
    public static func right(_ colors: [Color]) -> LinearGradient {
        linear(from: .leading, to: .trailing, colors: colors)
    }
    
    /// `Gradient.right(c1, c2, c3)`
    public static func right(_ colors: Color...) -> LinearGradient {
        right(colors)
    }
    
    /// `Gradient.left(colors)`
    public static func left(_ colors: [Color]) -> LinearGradient {
        linear(from: .trailing, to: .leading, colors: colors)
    }
    
    /// `Gradient.left(c1, c2, c3)`
    public static func left(_ colors: Color...) -> LinearGradient {
        left(colors)
    }
    
    
    /// `Gradient.bottomRight(colors)`
    /// - Parameters:
    ///   - colors: ๆผธๅฑค้ก่‰ฒ
    public static func bottomRight(_ colors: [Color]) -> LinearGradient {
        linear(from: .topLeading, to: .bottomTrailing, colors: colors)
    }
    
    /// Gradient.bottomRight(color1, color2, color3)
    public static func bottomRight(_ colors: Color...) -> LinearGradient {
        bottomRight(colors)
    }
    
    /// `Gradient.topLeft(colors)`
    public static func topLeft(_ colors: [Color]) -> LinearGradient {
        linear(from: .bottomTrailing, to: .topLeading, colors: colors)
    }
    
    /// `Gradient.topLeft(color1, color2, color3)`
    public static func topLeft(_ colors: Color...) -> LinearGradient {
        topLeft(colors)
    }
    
    
    /// `Gradient.topRight(colors)`
    /// - Parameters:
    ///   - colors: ๆผธๅฑค้ก่‰ฒ
    public static func topRight(_ colors: [Color]) -> LinearGradient {
        linear(from: .bottomLeading, to: .topTrailing, colors: colors)
    }
    
    /// Gradient.topRight(color1, color2, color3)
    public static func topRight(_ colors: Color...) -> LinearGradient {
        topRight(colors)
    }
    
    /// Gradient.bottomLeft(colors)
    public static func bottomLeft(_ colors: [Color]) -> LinearGradient {
        linear(from: .topTrailing, to: .bottomLeading, colors: colors)
    }
    
    /// Gradient.bottomLeft(color1, color2, color3)
    public static func bottomLeft(_ colors: Color...) -> LinearGradient {
        bottomLeft(colors)
    }
    
}

Last updated