Skip to content

Commit

Permalink
feat(models): add support for color variables with swatch model (#157)
Browse files Browse the repository at this point in the history
Fixes #141

Co-authored-by: Lindsay Evans <lindsay.evans@boomworks.com.au>
  • Loading branch information
halffullheart and lindsayevans committed Jun 11, 2021
1 parent b205ae6 commit b322786
Show file tree
Hide file tree
Showing 14 changed files with 657 additions and 372 deletions.
852 changes: 481 additions & 371 deletions docs/models.md

Large diffs are not rendered by default.

7 changes: 6 additions & 1 deletion models/Color/Color.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,19 @@ class Color {

/**
*
* @param {String|Object} args This is passed to tinycolor 2. As long as tinycolor2 can understand this argument, the color will work.
* @param {String|Object|Color} args If a Color, used as-is, otherwised passed to tinycolor2. As long as tinycolor2 can understand this argument, the color will work.
* @param {Color.Model} json
*/
constructor(args, json) {
if (json) {
Object.assign(this, json);
} else {
const color = TinyColor(args || '#000').toRgb();

if (args && args.swatchID) {
this.swatchID = args.swatchID;
}

Object.assign(this, Color.Model, {
alpha: color.a,
blue: color.b / 255,
Expand Down
18 changes: 18 additions & 0 deletions models/Color/Color.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
*/

const Color = require('./index');
const { Swatch } = require('../index');

const json = {
_class: 'color',
Expand Down Expand Up @@ -42,4 +43,21 @@ describe('Color', () => {
color.lighten(100);
expect(color.toHexString()).toEqual('#ffffff');
});

it('should accept swatchID as property along with color components', () => {
const swatchID = '6D02695C-C1FF-471B-9948-A13985E7618E';
const alpha = 0.77;
const color = new Color({ swatchID, r: 1, g: 0, b: 0, a: alpha });
expect(color.swatchID).toEqual(swatchID);
expect(color.alpha).toEqual(alpha);
expect(color.blue).toEqual(0);
});

it('should accept result of swatch.asColor()', () => {
const swatch = new Swatch({ color: new Color('purple') });
const refColor = swatch.asColor();
const color = new Color(refColor);
expect(color.swatchID).toEqual(swatch.do_objectID);
expect(color.red).toEqual(refColor.r / 255);
});
});
1 change: 1 addition & 0 deletions models/Color/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ declare class Color {
blue: number;
green: number;
red: number;
swatchID?: string;

constructor(args?: any, json?: any);

Expand Down
21 changes: 21 additions & 0 deletions models/Document/Document.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ class Document {
_class: 'sharedTextStyleContainer',
objects: [],
},
sharedSwatches: {
_class: 'swatchContainer',
objects: [],
},
pages: [],
};
}
Expand Down Expand Up @@ -119,6 +123,23 @@ class Document {
return this;
}

/**
* @returns {Swatch[]} An array of Swatches
*/
getSwatches() {
return this.sharedSwatches.objects;
}

/**
*
* @param {Swatch} swatch
* @returns {this}
*/
addSwatch(swatch) {
this.sharedSwatches.objects = this.sharedSwatches.objects.concat(swatch);
return this;
}

/**
*
* @param {String} pageID
Expand Down
9 changes: 9 additions & 0 deletions models/Document/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Color from '../Color';
import Page from '../Page';
import SharedStyle from '../SharedStyle';
import Swatch from '../Swatch';

declare class Document {
_class: 'document';
Expand Down Expand Up @@ -32,6 +33,10 @@ declare class Document {
_class: 'sharedTextStyleContainer';
objects: SharedStyle[];
};
sharedSwatches: {
_class: 'swatchContainer';
objects: Swatch[];
};
pages: Page[];

constructor(args?: any, json?: any);
Expand All @@ -46,6 +51,10 @@ declare class Document {

addTextStyle(style: SharedStyle): Document;

getSwatches(): Swatch[];

addSwatch(swatch: Swatch): Document;

addPage(pageID: string): Document;
}

Expand Down
8 changes: 8 additions & 0 deletions models/Sketch/Sketch.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,14 @@ class Sketch {
return this.document.getTextStyles();
}

addSwatch(swatch) {
this.document.addSwatch(swatch);
}

getSwatches() {
return this.document.getSwatches();
}

addPage(page, args) {
if (!(page instanceof Page)) {
page = new Page(page);
Expand Down
5 changes: 5 additions & 0 deletions models/Sketch/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import User from '../User';
import Document from '../Document';
import Page from '../Page';
import SharedStyle from '../SharedStyle';
import Swatch from '../Swatch';

declare class Sketch {
static fromFile(path: fs.PathLike): Promise<Sketch>;
Expand Down Expand Up @@ -35,6 +36,10 @@ declare class Sketch {

getTextStyles(): SharedStyle[];

addSwatch(swatch: Swatch): void;

getSwatches(): Swatch[];

addPage(page: any, args?: any): void;

addArtboard(pageID: string, artboard: any): void;
Expand Down
45 changes: 45 additions & 0 deletions models/Swatch/Swatch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
const uuid = require('uuid-v4');
const Color = require('../Color/Color');

/**
* Also known as 'Color Variables'
*/
class Swatch {
static get Model() {
return {
_class: 'swatch',
};
}

/**
*
* @param {Object} args
* @param {String} args.name
* @param {Color} args.color
* @param {Color.Model} json
*/
constructor(args, json) {
if (json) {
Object.assign(this, Swatch.Model, json);
this.value = new Color(this.value);
} else {
const id = args.id || uuid().toUpperCase();
const name = args.name || args.color.toHex().toUpperCase();
Object.assign(this, Swatch.Model, {
name,
do_objectID: id,
value: args.color,
});
}
}

/**
* Get an object suitable for use in constructing colors
* @returns {Color} A color with the `swatchID` set
*/
asColor() {
return { ...this.value.toRgb(), swatchID: this.do_objectID };
}
}

module.exports = Swatch;
35 changes: 35 additions & 0 deletions models/Swatch/Swatch.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
const Color = require('../Color');
const Swatch = require('./index');

const json = {
name: 'Test Color',
value: new Color('#c9c'),
};

describe('Swatch', () => {
it('should work from raw JSON', () => {
const swatch = new Swatch(null, json);
expect(swatch._class).toBe('swatch');
});

it('should have a default name when no name is given', () => {
const lime = new Color('limegreen');
const unnamedSwatch = new Swatch({ color: lime });
expect(unnamedSwatch.name).toBe('32CD32');

const namedSwatch = new Swatch({ name: 'Lime', color: lime });
expect(namedSwatch.name).toBe('Lime');
});

it('asColor should work', () => {
const color = new Color('#F00');
const swatch = new Swatch({ color });
const refColor = swatch.asColor();
expect(refColor.swatchID).not.toBeUndefined();
expect(refColor.swatchID).toBe(swatch.do_objectID);
expect(refColor.r).toBe(255);
expect(refColor.g).toBe(0);
expect(refColor.b).toBe(0);
expect(refColor.a).toBe(1);
});
});
11 changes: 11 additions & 0 deletions models/Swatch/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Color } from '..';

declare class Swatch {
_class: 'swatch';
do_objectID: string;
value: Color;

constructor(args?: any, json?: any);
}

export = Swatch;
14 changes: 14 additions & 0 deletions models/Swatch/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
* Copyright 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with
* the License. A copy of the License is located at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
* and limitations under the License.
*/

module.exports = require('./Swatch');
2 changes: 2 additions & 0 deletions models/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import Slice from './Slice';
import Star from './Star';
import StringAttribute from './StringAttribute';
import Style from './Style';
import Swatch from './Swatch';
import SymbolInstance from './SymbolInstance';
import SymbolMaster from './SymbolMaster';
import Text from './Text';
Expand Down Expand Up @@ -72,6 +73,7 @@ export {
Star,
StringAttribute,
Style,
Swatch,
SymbolInstance,
SymbolMaster,
Text,
Expand Down
1 change: 1 addition & 0 deletions models/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ module.exports = {
Star: require('./Star'),
StringAttribute: require('./StringAttribute'),
Style: require('./Style'),
Swatch: require('./Swatch'),
SymbolInstance: require('./SymbolInstance'),
SymbolMaster: require('./SymbolMaster'),
Text: require('./Text'),
Expand Down

0 comments on commit b322786

Please sign in to comment.