Skip to content

Commit

Permalink
[web] Add color support 🎨 (#533)
Browse files Browse the repository at this point in the history
  • Loading branch information
EvanBacon authored and osdnk committed Jan 2, 2020
1 parent 13d37bb commit 757de23
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
4 changes: 3 additions & 1 deletion Example/colors/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ export default class Example extends Component {
static navigationOptions = {
title: 'Colors Example',
};

constructor(props) {
super(props);

Expand Down Expand Up @@ -118,6 +119,7 @@ export default class Example extends Component {
const v = 1;
this._color = colorHSV(h, s, v);
}

render() {
return (
<View style={styles.container}>
Expand All @@ -132,7 +134,7 @@ export default class Example extends Component {
{
backgroundColor: this._color,
transform: [
{ translateX: this._transX, translateY: this._transY },
{ translateX: this._transX }, { translateY: this._transY },
],
},
]}
Expand Down
12 changes: 7 additions & 5 deletions src/derived/color.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Platform } from 'react-native';

import { add, cond, lessThan, multiply, round, sub, proc } from '../base';
import { add, cond, concat, lessThan, multiply, round, sub, proc } from '../base';
import AnimatedNode from '../core/AnimatedNode';

const procColor = proc(function(r, g, b, a) {
Expand All @@ -10,6 +10,7 @@ const procColor = proc(function(r, g, b, a) {
multiply(g, 1 << 8),
b
);

if (Platform.OS === 'android') {
// on Android color is represented as signed 32 bit int
return cond(
Expand All @@ -22,15 +23,16 @@ const procColor = proc(function(r, g, b, a) {
});

export default function color(r, g, b, a = 1) {
if (Platform.OS === 'web') {
// doesn't support bit shifting
return concat('rgba(', r, ',', g, ',', b, ',', a, ')');
}

if (a instanceof AnimatedNode) {
a = round(multiply(a, 255));
} else {
a = Math.round(a * 255);
}

if (Platform.OS === 'web') {
throw new Error('color is not implemented on web yet');
}

return procColor(r, g, b, a);
}

0 comments on commit 757de23

Please sign in to comment.