Home > Blockchain >  Difference between Color.colorName and Color(.colorName)
Difference between Color.colorName and Color(.colorName)

Time:12-28

The blue color produced by Color.blue is different from the one produced by Color(.blue). Is this a bug in Xcode preview, or do they really present different values? This doesn't apply to blue only but to several colors such as "red". Screenshot of xcode showing the color difference

CodePudding user response:

Yes, the two are different. By command-clicking on the tokens and using Xcode's "Jump to definition" feature, you can see the headers where they are defined.

For Color.blue:

extension Color {
// ...
/// A context-dependent blue color suitable for use in UI elements.
public static let blue: Color

For Color(.blue):

open class UIColor {
// ...
open class var blue: UIColor { get } // 0.0, 0.0, 1.0 RGB

Note that Color.blue says "context-dependent" -- this will change with various system environments, such as light or dark mode.

Also relevant: https://developer.apple.com/documentation/uikit/uicolor/standard_colors

Also see note in the comments about a deprecation warning.

  • Related