Skip to content

Commit

Permalink
feat(📉): Improvements to the Path API (#377)
Browse files Browse the repository at this point in the history
  • Loading branch information
wcandillon authored Oct 19, 2020
1 parent 4e37646 commit af87fea
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 211 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
"react-native-reanimated": "2.0.0-alpha.6",
"semantic-release": "^15.13.3",
"semantic-release-cli": "^4.1.2",
"typescript": "^3.6.2"
"typescript": "4.0.3"
},
"react-native": "lib/module/index.js",
"module": "lib/module/index.js",
Expand Down
33 changes: 8 additions & 25 deletions src/Colors.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { Platform } from "react-native";
import {
interpolate,
Extrapolate,
Expand All @@ -7,15 +6,12 @@ import {

import { clamp, mix } from "./Math";

declare let _WORKLET: boolean;

/**
* @summary TypeScript type to define an animation value as color.
* @example
// Color can be of string or number depending of the context in which it was executed
const color: Animated.SharedValue<Color> = useDerivedValue(() => mixColor(progress.value, "blue", "red"));
*/
export type Color = string | number;
export enum ColorSpace {
RGB,
HSV,
Expand Down Expand Up @@ -46,22 +42,9 @@ export const blue = (c: number) => {
return c & 255;
};

export const color = (r: number, g: number, b: number, alpha = 1): Color => {
export const color = (r: number, g: number, b: number, alpha = 1): string => {
"worklet";
if (Platform.OS === "web" || !_WORKLET) {
return `rgba(${r}, ${g}, ${b}, ${alpha})`;
}
const a = alpha * 255;
const c =
a * (1 << 24) +
Math.round(r) * (1 << 16) +
Math.round(g) * (1 << 8) +
Math.round(b);
if (Platform.OS === "android") {
// on Android color is represented as signed 32 bit int
return c < (1 << 31) >>> 0 ? c : c - Math.pow(2, 32);
}
return c;
return `rgba(${r}, ${g}, ${b}, ${alpha})`;
};

/**
Expand Down Expand Up @@ -98,7 +81,7 @@ export const hsv2rgb = (h: number, s: number, v: number) => {
/**
* @summary Convert HSV to RGB
*/
export const hsv2color = (h: number, s: number, v: number): Color => {
export const hsv2color = (h: number, s: number, v: number) => {
"worklet";
const { r, g, b } = hsv2rgb(h, s, v);
return color(r, g, b);
Expand Down Expand Up @@ -233,9 +216,9 @@ const interpolateColorsRGB = (
export const interpolateColor = (
value: number,
inputRange: number[],
rawOutputRange: Color[],
rawOutputRange: string[],
colorSpace: ColorSpace = ColorSpace.RGB
): Color => {
) => {
"worklet";
const outputRange = rawOutputRange.map((c) =>
typeof c === "number" ? c : processColor(c)
Expand All @@ -256,10 +239,10 @@ export const interpolateColor = (
*/
export const mixColor = (
value: number,
color1: Color,
color2: Color,
color1: string,
color2: string,
colorSpace: ColorSpace = ColorSpace.RGB
): Color => {
) => {
"worklet";
return interpolateColor(value, [0, 1], [color1, color2], colorSpace);
};
Loading

0 comments on commit af87fea

Please sign in to comment.