1

My code currently consists of:

VStack{
   Text("Something")
}
.background(Color.red)

I want to use a custom color for the background instead of Color.red. However, looking at the documentation, it seems like the background function takes in a background. Therefore, I can't declare a variable:

let exampleColor : Color = Color(red: 141, green: 223, blue: 144)

and use it within the background function. How do I tackle this problem?

VStack{
   Text("Something")
}
.background(exampleColor)

Doesn't work.

Louis Ye
  • 134
  • 1
  • 13
  • 1
    I think you have to pass value between 0 to 1 for RGB Color like change `Color(red: 141, green: 223, blue: 144)` to `Color(red: 141/255, green: 223/255, blue: 144/255)` or use `Color(red: 1, green: 0.8, blue: 0)` – Kishan Bhatiya Aug 28 '20 at 04:38
  • Does this answer your question? [How to initialize UIColor from RGB values properly?](https://stackoverflow.com/questions/8023916/how-to-initialize-uicolor-from-rgb-values-properly) – pkamb Aug 28 '20 at 04:49

1 Answers1

4

You have to set values between 0 to 1 for RGB Colors. Use the following

let exampleColor : Color = Color(red: 141/255, green: 223/255, blue: 144/255)

or value between 0 to 1 like

let exampleColor : Color = Color(red: 0.5, green: 0.8, blue: 0.5)
Kishan Bhatiya
  • 2,175
  • 8
  • 14