Skip to content

Commit

Permalink
Bump to 0.2.0. We can now choose between 3 color formats.
Browse files Browse the repository at this point in the history
  • Loading branch information
DAVID Amaury committed Jan 6, 2019
1 parent 3d1d6f3 commit 384a3af
Show file tree
Hide file tree
Showing 7 changed files with 143 additions and 55 deletions.
15 changes: 11 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Whether it is a web, iOS, Android or macOS project, **Swift for all** won't let

### Colors

All colors are defined in a struct in UIColor. By default the struct is named from the project but you can override its name in the extension's parameters.
All colors are defined in a struct in UIColor. By default the struct is named from the project but you can override it with the `Override color structure name` option.

```swift
extension UIColor {
Expand All @@ -35,11 +35,18 @@ extension UIColor {
view.backgroundColor = UIColor.MyProject.pink
```

The `Prefer hex over rgb` option allows you to use hex values instead of rgb:
```
With the option `Color format declaration` you can use different color formatting:
```swift
//RGB (iOS & macOS default)
static let blue1 = UIColor(red: 68/255.0, green: 104/255.0, blue: 234/255.0, alpha: 1)

//Hex RGBA (like web)
static let blue1 = UIColor(rgbaValue:0x4468eaff)

//Hex ARGB (like Android)
static let blue1 = UIColor(argbValue:0xff4468ea)
```
Please note that the format is RGBA, which is the dominant format in web environments but differs from Android (ARGB).
Please note that the convenience UIColor hex init code doesn't appear in the snippet to keep it clean, but is actually included in the file if you export colors.

### Text styles

Expand Down
21 changes: 16 additions & 5 deletions dist/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,16 @@ You are an Swift developer and you are working on an app which is only available
Whether it is a web, iOS, Android or macOS project, **Swift for all** won't let you down.

## Getting Stated
1. Go to https://extensions.zeplin.io/
2. Search "Swift for all" in the extension
3. Click on "Add to Zeplin"
1. Go to https://extensions.zeplin.io/5c23c811a493040a8ba65679
2. Click on "Add to Zeplin"

## Usage

**Swift for all** currently only generates Swift code for your project's styleguide. Snippets are not available when selecting layers in a screen.

### Colors

All colors are defined in a struct in UIColor. By default the struct is named from the project but you can override its name in the extension's parameters.
All colors are defined in a struct in UIColor. By default the struct is named from the project but you can override it with the `Override color structure name` option.

```swift
extension UIColor {
Expand All @@ -36,6 +35,19 @@ extension UIColor {
view.backgroundColor = UIColor.MyProject.pink
```

With the option `Color format declaration` you can use different color formatting:
```swift
//RGB (iOS & macOS default)
static let blue1 = UIColor(red: 68/255.0, green: 104/255.0, blue: 234/255.0, alpha: 1)

//Hex RGBA (like web)
static let blue1 = UIColor(rgbaValue:0x4468eaff)

//Hex ARGB (like Android)
static let blue1 = UIColor(argbValue:0xff4468ea)
```
Please note that the convenience UIColor hex init code doesn't appear in the snippet to keep it clean, but is actually included in the file if you export colors.

### Text styles

Unlike the original Swift extension which only generate code for fonts, **Swift for all** generates the actuel text style by using all available parameters: font, font size, line height, text color, letter spacing, etc.
Expand Down Expand Up @@ -99,7 +111,6 @@ case .body:
## What's next ?

Here is a non-exhaustive list of future features to come:
- [ ] Shorten the color declaration by using an extension
- [ ] Add the possibility to use `NSAttributedString.Key.lineSpacing` instead of `NSAttributedString.Key.minimumLineHeight`
- [ ] Generate screen snippets on text layers
- [ ] Select the Swift version as with the original Swift extension
Expand Down
1 change: 1 addition & 0 deletions dist/main.251680fc.js

Large diffs are not rendered by default.

64 changes: 42 additions & 22 deletions dist/main.js

Large diffs are not rendered by default.

26 changes: 20 additions & 6 deletions dist/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"packageName": "SwiftForAll",
"name": "SwiftForAll",
"description": "Generates Swift styleguide on all project types.",
"version": "0.1.1",
"version": "0.2.0",
"author": {
"name": "Amaury DAVID"
},
Expand All @@ -14,10 +14,24 @@
"default": ""
},
{
"name": "Prefer hex over rgb",
"type": "switch",
"id": "colorPreferHex",
"default": false
"name": "Color format declaration",
"type": "picker",
"id": "colorFormat",
"options": [
{
"name": "RGB (iOS & macOS default)",
"value": "rgb"
},
{
"name": "Hex RGBA (like web)",
"value": "hex_rgba"
},
{
"name": "Hex ARGB (like Android)",
"value": "hex_argb"
}
],
"default": "rgb"
},
{
"name": "Font format in text styles",
Expand All @@ -42,7 +56,7 @@
"osx",
"web"
],
"moduleURL": "./main.00c114cb.js",
"moduleURL": "./main.251680fc.js",
"repository": "https://github.com///github.com/amaurydavid/SwiftForAll",
"readmeURL": "./README.md"
}
24 changes: 19 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "SwiftForAll",
"version": "0.1.1",
"version": "0.2.0",
"description": "Generates Swift styleguide on all project types.",
"author": {
"name": "Amaury DAVID"
Expand Down Expand Up @@ -29,10 +29,24 @@
"default": ""
},
{
"name": "Prefer hex over rgb",
"type": "switch",
"id": "colorPreferHex",
"default": false
"name": "Color format declaration",
"type": "picker",
"id": "colorFormat",
"options": [
{
"name": "RGB (iOS & macOS default)",
"value": "rgb"
},
{
"name": "Hex RGBA (like web)",
"value": "hex_rgba"
},
{
"name": "Hex ARGB (like Android)",
"value": "hex_argb"
}
],
"default": "rgb"
},
{
"name": "Font format in text styles",
Expand Down
47 changes: 34 additions & 13 deletions src/swiftColors.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,15 @@ function getColorsSwiftFileContent(context, colors) {
var code = "import UIKit\n\n";
code += getColorsSwiftSnippet(context, colors);

//Add the UIColor hex init if needed
if (context.getOption("colorPreferHex") == true) {
code += "\n\n" + getSwiftHexColorExtension();
const colorFormat = context.getOption("colorFormat");
if (colorFormat == "rgb") {
return code;
}

//Add the UIColor hex init
const alphaFirst = context.getOption("colorFormat") == "hex_argb"
code += "\n\n" + getSwiftHexColorExtension(alphaFirst);

return code
}

Expand All @@ -40,12 +44,19 @@ function getExistingColorSwiftCode(context, color) {
}

function getColorSwiftCode(context, color) {
if (context.getOption("colorPreferHex") == true) {
const hexColor = color.toHex();
return `UIColor(rgbaValue:0x${hexColor.r}${hexColor.g}${hexColor.b}${hexColor.a})`;
} else {
const formatOption = context.getOption("colorFormat");
if (formatOption == "rgb") {
return `UIColor(red: ${color.r}/255.0, green: ${color.g}/255.0, blue: ${color.b}/255.0, alpha: ${color.a})`;
}

const hexColor = color.toHex();
if (formatOption == "hex_rgba") {
const parameterName = getSwiftHexInitParameterName(false);
return `UIColor(${parameterName}:0x${hexColor.r}${hexColor.g}${hexColor.b}${hexColor.a})`;
} else if (formatOption == "hex_argb") {
const parameterName = getSwiftHexInitParameterName(true);
return `UIColor(${parameterName}:0x${hexColor.a}${hexColor.r}${hexColor.g}${hexColor.b})`;
}
}

function getColorStructName(context) {
Expand All @@ -69,14 +80,24 @@ function getColorDeclarationSwiftCode(context, color) {
return `static let ${camelCase(color.name)} = ${getColorSwiftCode(context, color)}`;
}

function getSwiftHexColorExtension() {
function getSwiftHexInitParameterName(alphaFirst) {
return alphaFirst ? "argbValue" : "rgbaValue";
}

function getSwiftHexColorExtension(alphaFirst) {
const parameterName = getSwiftHexInitParameterName(alphaFirst);
const redShifintgValue = alphaFirst ? 16 : 24;
const greenShiftingValue = alphaFirst ? 8 : 16;
const blueShiftingValue = alphaFirst ? 0 : 8;
const alphaShiftingValue = alphaFirst ? 24 : 0;

return `
extension Color {
convenience init(rgbaValue: UInt32) {
let red = CGFloat((rgbaValue >> 24) & 0xff) / 255.0
let green = CGFloat((rgbaValue >> 16) & 0xff) / 255.0
let blue = CGFloat((rgbaValue >> 8) & 0xff) / 255.0
let alpha = CGFloat((rgbaValue ) & 0xff) / 255.0
convenience init(${parameterName}: UInt32) {
let red = CGFloat((${parameterName} >> ${redShifintgValue}) & 0xff) / 255.0
let green = CGFloat((${parameterName} >> ${greenShiftingValue}) & 0xff) / 255.0
let blue = CGFloat((${parameterName} >> ${blueShiftingValue}) & 0xff) / 255.0
let alpha = CGFloat((${parameterName} >> ${alphaShiftingValue}) & 0xff) / 255.0
self.init(red: red, green: green, blue: blue, alpha: alpha)
}
Expand Down

0 comments on commit 384a3af

Please sign in to comment.