Skip to content
This repository has been archived by the owner on Sep 6, 2018. It is now read-only.

Color template with literals #73

Merged
merged 7 commits into from
Aug 20, 2017
Merged

Color template with literals #73

merged 7 commits into from
Aug 20, 2017

Conversation

djbe
Copy link
Member

@djbe djbe commented Aug 17, 2017

Partially inspired by #72 and an old discussion #55 (comment).


```swift
extension ColorName {
/// 0x339666ff (r: 20%, g: 58%, b: 40%, a: 100%)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still think that a lot of people dealing with colors palettes provided by their designers (e.g. via a Sketch document or a sheet listing all colors of the app theme, etc) generally deal either with hex or with rgba(51, 150, 104, 255) and less commonly with % for rgb values 😉


| Parameter Name | Default Value | Description |
| -------------- | ------------- | ----------- |
| `enumName` | `ColorName` | Allows you to change the name of the generated `enum` containing all colors. |
Copy link
Collaborator

@AliSoftware AliSoftware Aug 17, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a note (same in the documentations for other literals-swiftN of course) about the special Color name (maybe outside the table as it can be a paragraph of its own), e.g.

Note: if you use --param enumName=Color then the color constants will be generated as an extension of the UIColor (iOS) / NSColor (macOS) type directly without creating a separate enum type for namespacing those color constants

But tbf I think it would be less surprising if the test was on testing UIColor or NSColor, not Color.

Like adapting the template to:

{% set enumName %}{{param.enumName|default:"ColorName"}}{% endset %}
#if os(OSX)
  import AppKit.NSColor
  typealias Color = NSColor
  {% if enumName != 'NSColor' %}enum {{enumName}} { }{% endif %}
#elseif os(iOS) || os(tvOS) || os(watchOS)
  import UIKit.UIColor
  typealias Color = UIColor
  {% if enumName != 'UIColor' %}enum {{enumName}} { }{% endif %}
#endif

// swiftlint:disable file_length identifier_name line_length type_body_length
extension {{enumName}} {

}

And then explaining that in the doc that the exception is with --param enumName=UIColor for those who wish to make those constants as an extension of UIColor directly. Would feel more natural as a special name imho?

@AliSoftware
Copy link
Collaborator

💡 Once #68 is merged it could be nice to apply a similar idea for colors on the xcassets templates, making the named colors being directly static let foo = UIColor(name:) instead of using an intermediate struct type for colors (I wonder if #colorLiteral supports named colors btw? If not, we can just use UIColor(name: ) for that case)

@djbe
Copy link
Member Author

djbe commented Aug 17, 2017

#colorLiteral doesn't support names afaik.

I'm not sure if it's a good idea to have asset template variations just for that case, especially because there are so many more possible set types in catalogs we don't support yet. It would be better to decide now what'd be most useful for the user.

The thing we have to keep in mind is that named colors are iOS 11+ (macOS 10.13+), so I'm not even sure how that'd work? Place a whole bunch of #if available throughout the file?

@AliSoftware
Copy link
Collaborator

Ah, right, having to @available above every static let foo = UIColor(name:…) generated constant would be a bit too much probably.

## Usage example

```swift
// To reference a color, simpy reference it's static instance by name:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/it's/its/

## Usage example

```swift
// To reference a color, simpy reference it's static instance by name:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/it's/its/

test(template: "literals-swift3",
contextNames: Contexts.all,
directory: .colors,
contextVariations: variations)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we also test the --param enumName=UIColor case too, to check the enum isn't created there?

@djbe djbe merged commit 154dd2c into master Aug 20, 2017
@djbe djbe deleted the feature/color-literals branch August 20, 2017 16:06
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants