Permalink
Browse files

color names should be case insensitive

  • Loading branch information...
1 parent bd62024 commit 65bd79e63a70120501682ac484c8a8e73c55c5d4 @jongold jongold committed Apr 14, 2017
Showing with 28 additions and 14 deletions.
  1. +1 −0 .jestrc
  2. +6 −0 __tests__/jsonUtils/models.js
  3. +11 −12 src/jsonUtils/hacksForJSONImpl.js
  4. +10 −2 src/jsonUtils/models.js
View
@@ -2,6 +2,7 @@
"testEnvironment": "node",
"testPathIgnorePatterns": [
"/node_modules/",
+ "<rootDir>/_book",
"<rootDir>/lib",
"<rootDir>/scratch"
],
@@ -32,6 +32,12 @@ describe('makeColorFromCSS', () => {
expect(makeColorFromCSS('rebeccapurple')).toEqual(PURPLE);
});
+ it('is case-insensitive', () => {
+ expect(makeColorFromCSS('BLACK')).toEqual(BLACK);
+ expect(makeColorFromCSS('wHIte')).toEqual(WHITE);
+ expect(makeColorFromCSS('rebeccaPurple')).toEqual(PURPLE);
+ });
+
it('works with rgb colors', () => {
expect(makeColorFromCSS('rgb(0, 0, 0)')).toEqual(BLACK);
expect(makeColorFromCSS('rgb(255, 255, 255)')).toEqual(WHITE);
@@ -3,10 +3,9 @@
import type { SJTextStyle } from 'sketchapp-json-flow-types';
import { TextAlignment } from 'sketch-constants';
import { toSJSON } from 'sketchapp-json-plugin';
-import normalizeColor from 'normalize-css-color';
import findFont from '../utils/findFont';
import type { TextStyle } from '../types';
-import { generateID } from './models';
+import { generateID, makeColorFromCSS } from './models';
export const TEXT_ALIGN = {
auto: TextAlignment.Left,
@@ -57,16 +56,16 @@ export const makeImageDataFromUrl = (url: string): MSImageData => {
export function makeAttributedString(string: ?string, textStyle: TextStyle) {
const font = findFont(textStyle);
- const color = normalizeColor.rgba(normalizeColor(textStyle.color || 'black'));
+ const color = makeColorFromCSS(textStyle.color || 'black');
const attribs: Object = {
MSAttributedStringFontAttribute: font.fontDescriptor(),
NSParagraphStyle: makeParagraphStyle(textStyle),
NSColor: NSColor.colorWithDeviceRed_green_blue_alpha(
- color.r / 255,
- color.g / 255,
- color.b / 255,
- color.a,
+ color.red,
+ color.green,
+ color.blue,
+ color.alpha,
),
};
@@ -89,18 +88,18 @@ export function makeTextStyle(textStyle: TextStyle) {
const font = findFont(textStyle);
- const color = normalizeColor.rgba(normalizeColor(textStyle.color || 'black'));
+ const color = makeColorFromCSS(textStyle.color || 'black');
const value: SJTextStyle = {
_class: 'textStyle',
encodedAttributes: {
MSAttributedStringFontAttribute: encodeSketchJSON(font.fontDescriptor()),
NSColor: encodeSketchJSON(
NSColor.colorWithDeviceRed_green_blue_alpha(
- color.r / 255,
- color.g / 255,
- color.b / 255,
- color.a,
+ color.red,
+ color.green,
+ color.blue,
+ color.alpha,
),
),
NSParagraphStyle: encodeSketchJSON(pStyle),
View
@@ -22,9 +22,17 @@ export function generateID(): string {
return e7();
}
-// Takes 'white', '#fff', &c
+const safeToLower = (input: Color): Color => {
+ if (typeof input === 'string') {
+ return input.toLowerCase();
+ }
+
+ return input;
+};
+
+// Takes colors as CSS hex, name, rgb, rgba, hsl or hsla
export const makeColorFromCSS = (input: Color): SJColor => {
- const nullableColor: ?number = normalizeColor(input);
+ const nullableColor: ?number = normalizeColor(safeToLower(input));
const colorInt: number = nullableColor == null ? 0x00000000 : nullableColor;
const { r, g, b, a } = normalizeColor.rgba(colorInt);

0 comments on commit 65bd79e

Please sign in to comment.