Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fill a RoundedRectangle with custom color

 RoundedRectangle(cornerRadius: 8, style: .continuous)
    .foregroundColor(Color.init(red: 255, green: 245, blue: 158))

My rounded rectangle is all white. Why can't I initiate a custom color? Whereas Color.red works perfectly fine?

like image 333
alamodey Avatar asked Nov 21 '25 02:11

alamodey


2 Answers

Color.init(red: CGFloat, green: CGFloat, blue: CGFloat)

takes a 3 CGFloats with values between 0 and 1

What you need is…

Color(red: 255/255, green: 245/255, blue: 158/255)

Note that the .init isn't required in Swift

like image 79
Ashley Mills Avatar answered Nov 23 '25 01:11

Ashley Mills


You can write an extension to take Ints as parameters to Color initializer. It will allow you to use Color(red: 255, green: 245, blue: 158):

extension Color {

    public init(red: Int, green: Int, blue: Int) {
        self = Color(red: Double(red)/255.0,
                     green: Double(green)/255.0,
                     blue: Double(blue)/255.0)
    }
}
like image 30
LuLuGaGa Avatar answered Nov 23 '25 01:11

LuLuGaGa