Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add DynamicColorIOS support #1

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions android/src/main/java/com/horcrux/svg/SvgView.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@

import androidx.annotation.NonNull;

import com.facebook.react.bridge.ColorPropConverter;
import com.facebook.react.bridge.Dynamic;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.uimanager.DisplayMetricsHolder;
import com.facebook.react.uimanager.ReactCompoundView;
import com.facebook.react.uimanager.ReactCompoundViewGroup;
Expand Down Expand Up @@ -173,11 +175,17 @@ private void clearChildCache() {
}

@ReactProp(name = "tintColor")
public void setTintColor(@Nullable Integer tintColor) {
if (tintColor == null) {
mTintColor = 0;
} else {
mTintColor = tintColor;
public void setTintColor(@Nullable Dynamic tintColor) {
switch (tintColor.getType()) {
case Null:
mTintColor = 0;
break;
case Map:
mTintColor = ColorPropConverter.getColor(tintColor.asMap(), getContext());
break;
case Number:
mTintColor = tintColor.asInt();
break;
}
invalidate();
clearChildCache();
Expand Down
4 changes: 2 additions & 2 deletions android/src/main/java/com/horcrux/svg/SvgViewManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,12 @@ public boolean needsCustomLayoutForChildren() {
}

@ReactProp(name = "tintColor")
public void setTintColor(SvgView node, @Nullable Integer tintColor) {
public void setTintColor(SvgView node, @Nullable Dynamic tintColor) {
node.setTintColor(tintColor);
}

@ReactProp(name = "color")
public void setColor(SvgView node, @Nullable Integer color) {
public void setColor(SvgView node, @Nullable Dynamic color) {
node.setTintColor(color);
}

Expand Down
9 changes: 2 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 24 additions & 1 deletion src/elements/Svg.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
ResponderProps,
StrokeProps,
TransformProps,
ProcessedDynamicColor,
} from '../lib/extract/types';
import extractResponder from '../lib/extract/extractResponder';
import extractViewBox from '../lib/extract/extractViewBox';
Expand Down Expand Up @@ -183,7 +184,29 @@ export default class Svg extends Shape<

extractResponder(props, props, this as ResponderInstanceProps);

const tint = extractColor(color);
let tint = extractColor(color);
if (typeof color === 'object' && color !== null) {
if ('dynamic' in color) {
let processedColor: ProcessedDynamicColor = {
dynamic: {
light: extractColor(color.dynamic.light) as number,
dark: extractColor(color.dynamic.dark) as number,
},
};
if (color.dynamic.highContrastLight) {
processedColor.dynamic.highContrastLight = extractColor(
color.dynamic.highContrastLight,
) as number;
}
if (color.dynamic.highContrastDark) {
processedColor.dynamic.highContrastDark = extractColor(
color.dynamic.highContrastDark,
) as number;
}
tint = processedColor;
}
}

if (tint != null) {
props.color = tint;
props.tintColor = tint;
Expand Down
73 changes: 65 additions & 8 deletions src/lib/extract/extractBrush.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Platform } from 'react-native';
import extractColor, { integerColor } from './extractColor';
import { Color } from './types';
import { Color, ProcessedDynamicColor } from './types';

const urlIdPattern = /^url\(#(.+)\)$/;

Expand Down Expand Up @@ -39,15 +40,71 @@ export default function extractBrush(color?: Color) {
if (typeof int32ARGBColor === 'number') {
return int32ARGBColor;
}

// iOS PlatformColor
if ('semantic' in color) {
return [0, color];
if (typeof color === 'string') {
if (color.indexOf('platform|') === 0) {
return [
0,
Platform.select({
ios: { semantic: color.split('|').slice(1) },
android: { resource_paths: color.split('|').slice(1) },
}),
];
} else if (color.indexOf('semantic|') === 0) {
return [0, { semantic: color.split('|').slice(1) }];
} else if (color.indexOf('resource_paths|') === 0) {
return [0, { resource_paths: color.split('|').slice(1) }];
} else if (color.indexOf('dynamic|') === 0) {
let items = color.split('|');
let processedColor: ProcessedDynamicColor = {
dynamic: {
light: extractColor(items[1]) as number,
dark: extractColor(items[2]) as number,
},
};
if (items[3]) {
processedColor.dynamic.highContrastLight = extractColor(
items[3],
) as number;
}
if (items[4]) {
processedColor.dynamic.highContrastDark = extractColor(
items[4],
) as number;
}
return [0, processedColor];
}
}

// Android PlatformColor
if ('resource_paths' in color) {
return [0, color];
if (typeof color === 'object' && color !== null) {
// iOS PlatformColor
if ('semantic' in color) {
return [0, color];
}

if ('dynamic' in color) {
let processedColor: ProcessedDynamicColor = {
dynamic: {
light: extractColor(color.dynamic.light) as number,
dark: extractColor(color.dynamic.dark) as number,
},
};
if (color.dynamic.highContrastLight) {
processedColor.dynamic.highContrastLight = extractColor(
color.dynamic.highContrastLight,
) as number;
}
if (color.dynamic.highContrastDark) {
processedColor.dynamic.highContrastDark = extractColor(
color.dynamic.highContrastDark,
) as number;
}
return [0, processedColor];
}

// Android PlatformColor
if ('resource_paths' in color) {
return [0, color];
}
}

console.warn(`"${color}" is not a valid color or brush`);
Expand Down
18 changes: 17 additions & 1 deletion src/lib/extract/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,23 @@ export type NumberArray = NumberProp[] | NumberProp;
export type rgbaArray = number[];
// int32ARGBColor = 0xaarrggbb
export type Int32ARGBColor = number;
export type Color = Int32ARGBColor | rgbaArray | string;
export type Color = Int32ARGBColor | rgbaArray | string | DynamicColor;
export type DynamicColor = {
dynamic: {
light: Color;
dark: Color;
highContrastLight?: Color;
highContrastDark?: Color;
};
};
export type ProcessedDynamicColor = {
dynamic: {
light: Int32ARGBColor;
dark: Int32ARGBColor;
highContrastLight?: Int32ARGBColor;
highContrastDark?: Int32ARGBColor;
};
};

export type Linecap = 'butt' | 'square' | 'round';
export type Linejoin = 'miter' | 'bevel' | 'round';
Expand Down