Skip to content
This repository was archived by the owner on Jun 3, 2022. It is now read-only.

Commit 40f673f

Browse files
authored
Convert colors to rgba during serialization. Closes #6. (#60)
* Initial check-in of color rgba conversion code. * Updates color variable and rendering to use TinyColor library. * Adds back missing controlType for color. * Updates check for equal colors.
1 parent 8949d8d commit 40f673f

File tree

4 files changed

+35
-13
lines changed

4 files changed

+35
-13
lines changed

package.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
"@types/react-dom": "^0.14.19",
3838
"@types/sinon": "^1.16.32",
3939
"@types/sinon-chai": "^2.7.27",
40+
"@types/tinycolor2": "^1.1.0",
4041
"chai": "^3.5.0",
4142
"codecov.io": "^0.1.6",
4243
"copyfiles": "^1.0.0",
@@ -68,12 +69,13 @@
6869
"ts-loader": "^1.2.2",
6970
"tslint": "^4.0.2",
7071
"typescript": "^2.0.10",
71-
"webpack": "^2.1.0-beta.27",
72-
"webpack-dev-server": "^2.1.0-beta.12"
72+
"webpack": "^2.2.0-rc.3",
73+
"webpack-dev-server": "^2.2.0-rc.0"
7374
},
7475
"dependencies": {
7576
"material-design-lite": "^1.2.1",
7677
"react": "^15.4.0",
77-
"react-dom": "^15.4.0"
78+
"react-dom": "^15.4.0",
79+
"tinycolor2": "^1.4.1"
7880
}
7981
}

src/core/variables/ColorVariable.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
* under the License.
1515
*/
1616

17+
import * as TinyColor from "tinycolor2";
18+
1719
import { ConstraintType, ControlType, DataType } from "../../lib/Constants";
1820
import { ISerializableData } from "../../lib/LocalStorage";
1921
import { IVariableCallback, IVariableListParams, Variable } from "./Variable";
@@ -40,10 +42,10 @@ export class ColorVariable extends Variable implements IColorVariableParams {
4042
/**
4143
* Creates an instance of a ColorVariable.
4244
* @constructor
43-
* @param {string} key A unique key for the Variable.
44-
* @param {string} defaultValue The default value.
45+
* @param {string} key A unique key for the Variable.
46+
* @param {string} defaultValue The default value.
4547
* @param {string[]} limitedToValues The array of allowed values.
46-
* @param {IVariableCallback} callback The callback to invoke when updated.
48+
* @param {IVariableCallback} callback The callback to invoke when updated.
4749
* @return {ColorVariable}
4850
*/
4951
constructor(
@@ -97,8 +99,10 @@ export class ColorVariable extends Variable implements IColorVariableParams {
9799
*/
98100
serialize(): ISerializableData {
99101
let data = super.serialize();
100-
data.selectedValue = this.selectedValue;
101-
data.limitedToValues = this.limitedToValues;
102+
data.selectedValue = TinyColor(this.selectedValue).toRgb();
103+
data.limitedToValues = this.limitedToValues.map((value: any) => {
104+
return TinyColor(value).toRgb();
105+
});
102106
return data;
103107
}
104108

@@ -109,6 +113,10 @@ export class ColorVariable extends Variable implements IColorVariableParams {
109113
* @return {ColorVariable} A new initialized ColorVariable.
110114
*/
111115
static deserialize(data: ISerializableData): Variable {
112-
return new ColorVariable(data.key, data.selectedValue, data.limitedToValues);
116+
let selectedValue = TinyColor(data.selectedValue).toHexString();
117+
let limitedToValues = data.limitedToValues.map((color: string) => {
118+
return TinyColor(color).toHexString();
119+
});
120+
return new ColorVariable(data.key, selectedValue, limitedToValues);
113121
}
114122
}

src/ui/controls/ColorSwatchControl.tsx

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616

1717
import * as React from "react";
18+
import * as TinyColor from "tinycolor2";
1819

1920
import { CSS } from "../../lib/Constants";
2021
import { IColorControlProps } from "./controlProps";
@@ -52,12 +53,17 @@ export class ColorSwatchControl extends React.Component<IColorControlProps, void
5253
<div className={`${CSS.RMX_COLOR_SWATCH} ${CSS.MDL_LIST_ITEM} ${CSS.MDL_TWO_LINE}`}>
5354
<span className={CSS.MDL_PRIMARY}>
5455
<span>{title}
55-
<span className={CSS.RMX_SELECTED_VALUE}>{selectedValue}</span>
56+
<span className={CSS.RMX_SELECTED_VALUE}>
57+
{TinyColor(selectedValue).toString()}
58+
</span>
5659
</span>
5760
<span className={CSS.MDL_SECONDARY}>
5861
{limitedToValues.map((value: string) => (
5962
<ColorSwatch color={value} key={value}
60-
isSelected={selectedValue === value}
63+
isSelected={
64+
TinyColor(selectedValue).toRgbString() ===
65+
TinyColor(value).toRgbString()
66+
}
6167
onClick={this.onClick}
6268
/>
6369
))}
@@ -88,14 +94,20 @@ function ColorSwatch(props: IColorSwatchProps) {
8894
isSelected,
8995
onClick,
9096
} = props;
97+
// Determine a readable color to prevent a white checkmark on a light
98+
// color swatch.
99+
let readableCheckColors = [TinyColor("white"), TinyColor("gray")];
100+
let checkColor = TinyColor.mostReadable(TinyColor(color), readableCheckColors);
91101
return (
92102
<div
93103
className={CSS.RMX_COLOR_SWATCH_ITEM}
94104
style={{backgroundColor: color}}
95105
data-value={color}
96106
onClick={onClick}
97107
>
98-
{isSelected ? <i className="material-icons">check</i> : ""}
108+
{
109+
isSelected ? <i className="material-icons" style={{color: checkColor}}>check</i> : ""
110+
}
99111
</div>
100112
);
101113
}

webpack.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ module.exports = {
4242
inline: true,
4343
},
4444
performance: {
45-
hints: !IS_DEV
45+
hints: IS_DEV ? false : "warning"
4646
},
4747
resolve: {
4848
extensions: ['.webpack.js', '.web.js', '.ts', '.tsx', '.js']

0 commit comments

Comments
 (0)