Skip to content

Commit

Permalink
Improved variables (#2)
Browse files Browse the repository at this point in the history
* 0.3.0
- variables can now have custom opacity
- added support for single line comments
  • Loading branch information
nesium authored Aug 21, 2019
1 parent dc853da commit ead55a5
Show file tree
Hide file tree
Showing 30 changed files with 707 additions and 94 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,21 @@ This project allows you to create Xcode Assets catalogs for your colors. Instead
Turn this…

```
// Basic colors
$white: #ffffff
$black: #000000
$black50: #000000 50%
$classic: (light: $black, dark: $white)
// Accent colors
$brightAccent: #5753CF
$mediumBright: rgba(25, 200, 255, 1)
$mediumBrightHighlight: #70D1FA
// Greys
$grey1: $black
// Declarations
Text {
Primary: (light: #151618, dark: #E7E8EA)
Secondary: (light: $grey1, dark: #85868A)
Expand All @@ -28,7 +33,7 @@ NumericInput {
NumericKey {
Background: (light: $white, dark: #434343)
Highlight: (light: #C4CCDA, dark: #666666)
Shadow: (light: #848587, dark: $black)
Shadow: (light: #848587, dark: $black50 50%) // Apply alpha to variables
Text: $classic
}
Expand Down Expand Up @@ -82,6 +87,7 @@ extension UIColor {
```

### Installation via Homebrew

`brew install nesium/tools/xcode-color-assets`

### Usage
Expand Down
29 changes: 17 additions & 12 deletions asset-catalog/src/asset_catalog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use super::ColorSpace;
use super::Error;
use parser::ast::{
Color, ColorSet, ColorSetValue, Declaration, Document, DocumentItem, RuleSet, RuleSetItem, Value,
Variable,
};
use serde_json::json;
use std::collections::HashMap;
Expand All @@ -17,7 +18,7 @@ fn path_to_str(path: &Path) -> String {
}

enum ResolvedVariable<'a> {
Color(&'a Color),
Color(Color),
ColorSet(&'a ColorSet),
}

Expand All @@ -27,14 +28,14 @@ struct Config<'a> {
}

impl<'a> Config<'a> {
fn resolve_variable(&self, identifier: &str) -> Result<ResolvedVariable<'a>, Error> {
let value = match self.var_lookup.get(identifier) {
fn resolve_variable(&self, variable: &Variable) -> Result<ResolvedVariable<'a>, Error> {
let value = match self.var_lookup.get(&variable.identifier) {
Some(value) => value,
None => return Err(Error::UnknownIdentifier(identifier.to_string())),
None => return Err(Error::UnknownIdentifier(variable.identifier.to_string())),
};
match value {
Value::Variable(identifier) => self.resolve_variable(identifier),
Value::Color(color) => Ok(ResolvedVariable::Color(color)),
Value::Color(color) => Ok(ResolvedVariable::Color(variable.resolve_against(color))),
Value::ColorSet(colorset) => Ok(ResolvedVariable::ColorSet(colorset)),
}
}
Expand Down Expand Up @@ -187,20 +188,24 @@ fn write_declaration(
let append_colorset =
|value: &mut serde_json::value::Value, colorset: &ColorSet| -> Result<(), Error> {
match colorset.light {
ColorSetValue::Variable(ref identifier) => match config.resolve_variable(identifier)? {
ResolvedVariable::Color(color) => append_light_color(value, color)?,
ColorSetValue::Variable(ref variable) => match config.resolve_variable(variable)? {
ResolvedVariable::Color(color) => append_light_color(value, &color)?,
ResolvedVariable::ColorSet(_) => {
return Err(Error::AssignColorSetToLightProperty(identifier.to_string()))
return Err(Error::AssignColorSetToLightProperty(
variable.identifier.to_string(),
))
}
},
ColorSetValue::Color(ref color) => append_light_color(value, color)?,
}

match colorset.dark {
ColorSetValue::Variable(ref identifier) => match config.resolve_variable(identifier)? {
ResolvedVariable::Color(color) => append_dark_color(value, color)?,
ColorSetValue::Variable(ref variable) => match config.resolve_variable(variable)? {
ResolvedVariable::Color(color) => append_dark_color(value, &color)?,
ResolvedVariable::ColorSet(_) => {
return Err(Error::AssignColorSetToDarkProperty(identifier.to_string()))
return Err(Error::AssignColorSetToDarkProperty(
variable.identifier.to_string(),
))
}
},
ColorSetValue::Color(ref color) => append_dark_color(value, color)?,
Expand All @@ -211,7 +216,7 @@ fn write_declaration(

match declaration.value {
Value::Variable(ref identifier) => match config.resolve_variable(identifier)? {
ResolvedVariable::Color(color) => append_light_color(&mut info, color)?,
ResolvedVariable::Color(color) => append_light_color(&mut info, &color)?,
ResolvedVariable::ColorSet(colorset) => append_colorset(&mut info, colorset)?,
},
Value::Color(ref color) => append_light_color(&mut info, color)?,
Expand Down
113 changes: 67 additions & 46 deletions asset-catalog/tests/asset_catalog.rs
Original file line number Diff line number Diff line change
@@ -1,57 +1,26 @@
use asset_catalog::{write_asset_catalog, ColorSpace};
use parser::parse_document;
use parser::{ast::Document, parse_document};
use tempdir::TempDir;

#[test]
fn asset_catalog() {
let contents = r#"
$white: #ffffff
$black: #000000
$classic: (light: $black, dark: $white)
$brightAccent: #5753CF
$mediumBright: rgba(25, 200, 255, 1)
$mediumBrightHighlight: #70D1FA
$grey1: $black
Text {
Primary: (light: #151618, dark: #E7E8EA)
Secondary: (light: $grey1, dark: #85868A)
}
LightContentSeparator: (light: #F1F2F2, dark: #222525)
NumericInput {
NumericKey {
Background: (light: $white, dark: #434343)
Highlight: (light: #C4CCDA, dark: #666666)
Shadow: (light: #848587, dark: $black)
Text: $classic
}
DoneKey {
Background: (light: $mediumBright, dark: $brightAccent)
Highlight: (light: $mediumBrightHighlight, dark: rgba(103, 122, 219, 1))
Shadow: (light: #6E7073, dark: $black)
Text: $classic
}
Background: (light: #D6D9DE 30%, dark: #313131 40%)
}
"#;

let document = parse_document(contents.to_string()).expect("Could not parse document");

fn srgb_asset_catalog() {
let tmp_dir_srgb = TempDir::new("asset_catalog_srgb").expect("Create temp dir failed");
write_asset_catalog(&document, &tmp_dir_srgb.path(), ColorSpace::SRGB, true)
.expect("Could not write asset catalog");
write_asset_catalog(
&test_document(),
&tmp_dir_srgb.path(),
ColorSpace::SRGB,
true,
)
.expect("Could not write asset catalog");
assert!(!dir_diff::is_different(&tmp_dir_srgb.path(), "tests/fixtures/SRGB.xcassets").unwrap());
}

#[test]
fn display_p3_asset_catalog() {
let tmp_dir_display_p3 =
TempDir::new("asset_catalog_display_p3").expect("Create temp dir failed");
write_asset_catalog(
&document,
&test_document(),
&tmp_dir_display_p3.path(),
ColorSpace::DisplayP3,
true,
Expand All @@ -62,11 +31,14 @@ fn asset_catalog() {
"tests/fixtures/DisplayP3.xcassets"
)
.unwrap());
}

#[test]
fn extended_linear_srgb_asset_catalog() {
let tmp_dir_extended_linear_srgb =
TempDir::new("asset_catalog_extended_linear_srgb").expect("Create temp dir failed");
write_asset_catalog(
&document,
&test_document(),
&tmp_dir_extended_linear_srgb.path(),
ColorSpace::ExtendedRangeLinearSRGB,
true,
Expand All @@ -77,11 +49,14 @@ fn asset_catalog() {
"tests/fixtures/ExtendedRangeLinearSRGB.xcassets"
)
.unwrap());
}

#[test]
fn extended_srgb_asset_catalog() {
let tmp_dir_extended_srgb =
TempDir::new("asset_catalog_extended_srgb").expect("Create temp dir failed");
write_asset_catalog(
&document,
&test_document(),
&tmp_dir_extended_srgb.path(),
ColorSpace::ExtendedRangeSRGB,
true,
Expand All @@ -93,3 +68,49 @@ fn asset_catalog() {
)
.unwrap());
}

fn test_document() -> Document {
let contents = r#"
// Basic colors
$white: #ffffff
$black: #000000
$black50: #000000 50%
$classic: (light: $black, dark: $white)
// Accent colors
$brightAccent: #5753CF
$mediumBright: rgba(25, 200, 255, 1)
$mediumBrightHighlight: #70D1FA
// Greys
$grey1: $black
// Declarations
Text {
Primary: (light: #151618, dark: #E7E8EA)
Secondary: (light: $grey1, dark: #85868A)
}
LightContentSeparator: (light: #F1F2F2, dark: #222525)
NumericInput {
NumericKey {
Background: (light: $white, dark: #434343)
Highlight: (light: #C4CCDA, dark: #666666)
Shadow: (light: #848587, dark: $black50 50%) // Apply alpha to variables
Text: $classic
}
DoneKey {
Background: (light: $mediumBright, dark: $brightAccent)
Highlight: (light: $mediumBrightHighlight, dark: rgba(103, 122, 219, 1))
Shadow: (light: #6E7073, dark: $black)
Text: $classic
}
Background: (light: #D6D9DE 30%, dark: #313131 40%)
}
"#;

parse_document(contents.to_string()).expect("Could not parse document")
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"color": {
"color-space": "display-p3",
"components": {
"alpha": "1.000",
"alpha": "0.250",
"blue": "0x00",
"green": "0x00",
"red": "0x00"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"color": {
"color-space": "extended-linear-srgb",
"components": {
"alpha": "1.000",
"alpha": "0.250",
"blue": "0x00",
"green": "0x00",
"red": "0x00"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"color": {
"color-space": "extended-srgb",
"components": {
"alpha": "1.000",
"alpha": "0.250",
"blue": "0x00",
"green": "0x00",
"red": "0x00"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"color": {
"color-space": "srgb",
"components": {
"alpha": "1.000",
"alpha": "0.250",
"blue": "0x00",
"green": "0x00",
"red": "0x00"
Expand Down
23 changes: 20 additions & 3 deletions parser/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub struct Document {
pub enum DocumentItem {
Variable(Declaration<Value>),
RuleSet(RuleSet),
Declaration(Declaration<Value>)
Declaration(Declaration<Value>),
}

#[derive(Debug, Clone, PartialEq)]
Expand All @@ -20,17 +20,23 @@ pub struct Color {

#[derive(Debug, Clone, PartialEq)]
pub enum Value {
Variable(String),
Variable(Variable),
Color(Color),
ColorSet(ColorSet),
}

#[derive(Debug, Clone, PartialEq)]
pub enum ColorSetValue {
Variable(String),
Variable(Variable),
Color(Color),
}

#[derive(Debug, Clone, PartialEq)]
pub struct Variable {
pub identifier: String,
pub opacity: f32,
}

#[derive(Debug, Clone, PartialEq)]
pub struct ColorSet {
pub light: ColorSetValue,
Expand Down Expand Up @@ -72,3 +78,14 @@ impl Into<Value> for ColorSetValue {
}
}
}

impl Variable {
pub fn resolve_against(&self, color: &Color) -> Color {
Color {
r: color.r,
g: color.g,
b: color.b,
a: color.a * self.opacity,
}
}
}
Loading

0 comments on commit ead55a5

Please sign in to comment.