Hi, I am participating in the swift student challenge, and I need some help. When I make a color by Color(red: , green: , blue: ), I calculate it by getting the rgb of the color that I want and dividing each of the colors by 255. For example, if red was 5, I would divide it by 255 to get 0.01960784313, which I put in the code for the red parameter. However, when I run the code, the color that appears is a bit different than the one I was expecting to see. Can someone please explain this?
Color not working properly
Please can you post some code, the colour expected and the colour you got :)
Freddie
I tried using absolute division rather than specifying manually calculated numbers and I think this is what you want.
Let me know if not, Freddie
import SwiftUI
let darkorange = Color(red: 213/255, green: 131/255, blue: 120/255)
struct ContentView: View {
var body: some View {
VStack {
Rectangle() .foregroundStyle(darkorange)
}.onAppear(perform:{
print(Double(213/255))
})
}
}
never mind I got the same result again :(
I think it might be because it is based off which Display I have connected. When I do the same with google colour picker it prints similar values so I think the code is working as expected but your display/image compression is subtly changing the colours. I would not worry too much about it!
@Pokka your screen shot is from "Color LCD", which is what Apple calls the built-in display on their laptop. By default, this uses the P3 color space. If you go to Settings/Displays, select the built-in display and change its Preset to "Internet & Web (sRGB)", the Digital Color Meter should show you the values you expect.
Alternatively, you can create the color in the P3 color space:
let darkorange = Color(.displayP3, red: 213/255, green: 131/255, blue: 120/255)
The default display gamut is sRGB, which isn't as wide as P3. So when you create your dark orange in sRGB, then display it in P3, it isn't as colorful as you'd expect.