-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(models): add support for color variables with swatch model (#157)
Fixes #141 Co-authored-by: Lindsay Evans <[email protected]>
- Loading branch information
1 parent
b205ae6
commit b322786
Showing
14 changed files
with
657 additions
and
372 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters