[swift-evolution] [Pitch] Hex color literals

Yonas Kolb Yonas4596 at hotmail.com
Sun Apr 30 01:46:12 CDT 2017


Hi Swift Evolution


Color literals in their current state are in practice only useful as an Xcode utility feature, because creating, editing, or reading them in code is a pain.
Currently the only autocomplete provided opens the color picker, which once a color is selected adds something like this to the code: #colorLiteral(red: 0.9254902005, green: 0.2352941185, blue: 0.1019607857, alpha: 1).
Xcode provides a nice color swatch to preview the color, but if viewing this in raw source from a git diff or a 3rd party editor it becomes hard to tell what the color is, or edit it, especially as the rgb components are a fraction of 1 instead of the more readable and standard 1-255.

I propose adding a hex initialiser like this #colorLiteral(hex: “DB8B09", alpha: 0.5). Hex values are the industry standard for colors which makes this feature much easier to use. Now we can actually grok what a color is in source code without reaching for a convertor or calculator, as well as being to define these by simply pasting in hex strings.

Possible alternative to the above syntax could be:

  *   hex string param name: #colorLiteral(hexString: "DB8B09", alpha: 1)
  *   include alpha as last 2 characters: #colorLiteral(hexString: "DB8B09FF")
  *   including #:  #colorLiteral(hexString: "#DB8B09", alpha: 1). This should probably be optionally allowed either way.

I’m proposing adding this new hex init in addition to the old one, so it’s still backwards compatible. While outside the scope of swift evolution, the goal would be to get this to also be the default way Xcode would write values from the color picker, as well as being able to render them in the first place.
Having an extra autocomplete with the new init hex would be useful too, so you can just provide a hex string without opening up a color picker, and I don't think this would require xcode support.

I’m unsure if this proposal would require changes in UIColor, NSColor or Xcode, so I’m not even sure this is the right place to propose such a thing.

I haven’t drafted a proposal yet, as I just wanted to check if there was any interest first, or if this is even feasible.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170430/6b1a726f/attachment.html>


More information about the swift-evolution mailing list