Skip to content

Commit 4697602

Browse files
committed
feat: gradient background
1 parent eb03ec0 commit 4697602

File tree

5 files changed

+20
-7
lines changed

5 files changed

+20
-7
lines changed

demo/index.html

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
<link rel="apple-touch-icon" sizes="180x180" href="/static/apple-touch-icon.png" />
3333
<link rel="icon" type="image/png" sizes="32x32" href="/static/favicon-32x32.png" />
3434
<link rel="icon" type="image/png" sizes="16x16" href="/static/favicon-16x16.png" />
35-
<link rel="manifest" href="/static/site.webmanifest" />
3635
<link rel="mask-icon" href="/static/safari-pinned-tab.svg" color="#5bbad5" />
3736
<meta name="msapplication-TileColor" content="#da532c" />
3837
<meta name="theme-color" content="#ffffff" />

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"avatar",
1111
"react-avatar"
1212
],
13-
"author": "Chill Lab",
13+
"author": "Dapi",
1414
"license": "MIT",
1515
"peerDependencies": {
1616
"react": ">=16.0.0"

src/index.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ export default class ReactNiceAvatar extends Component<NiceAvatarProps> {
3737
mouthStyle: PropTypes.oneOf(defaultOptions.mouthStyle),
3838
shirtStyle: PropTypes.oneOf(defaultOptions.shirtStyle),
3939
shirtColor: PropTypes.string,
40-
bgColor: PropTypes.string
40+
bgColor: PropTypes.string,
41+
isGradient: PropTypes.bool
4142
}
4243

4344
render() {

src/types.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ export interface AvatarConfig {
3232
mouthStyle?: MouthStyle,
3333
shirtStyle?: ShirtStyle,
3434
shirtColor?: string,
35-
bgColor?: string
35+
bgColor?: string,
36+
isGradient?: boolean
3637
}
3738

3839
export interface AvatarFullConfig extends AvatarConfig {

src/utils.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ interface DefaultOptions {
6363
mouthStyle: MouthStyle[],
6464
shirtStyle: ShirtStyle[],
6565
shirtColor: string[],
66-
bgColor: string[]
66+
bgColor: string[],
67+
gradientBgColor: string[]
6768
}
6869
export const defaultOptions: DefaultOptions = {
6970
sex: ["man", "woman"],
@@ -81,7 +82,14 @@ export const defaultOptions: DefaultOptions = {
8182
mouthStyle: ["laugh", "smile", "peace"],
8283
shirtStyle: ["hoody", "short", "polo"],
8384
shirtColor: ["#9287FF", "#6BD9E9", "#FC909F", "#F4D150", "#77311D"],
84-
bgColor: ["#9287FF", "#6BD9E9", "#FC909F", "#F4D150", "#E0DDFF", "#D2EFF3", "#FFEDEF", "#FFEBA4", "#506AF4", "#F48150", "#74D153"]
85+
bgColor: ["#9287FF", "#6BD9E9", "#FC909F", "#F4D150", "#E0DDFF", "#D2EFF3", "#FFEDEF", "#FFEBA4", "#506AF4", "#F48150", "#74D153"],
86+
gradientBgColor: [
87+
"linear-gradient(45deg, rgba(120,113,245,1) 0%, rgba(52,185,242,1) 100%)",
88+
"linear-gradient(45deg, rgba(242,127,52,1) 0%, rgba(242,200,52,1) 100%)",
89+
"linear-gradient(45deg, rgba(34,193,195,1) 0%, rgba(253,187,45,1) 100%)",
90+
"linear-gradient(90deg, rgba(148,187,233,1) 0%, rgba(238,174,202,1) 100%)",
91+
"linear-gradient(45deg, rgba(53,173,247,1) 0%, rgba(86,241,204,1) 100%)"
92+
]
8593
};
8694
export const genConfig: GenConfigFunc = (userConfig = {}) => {
8795
const response = {} as Required<AvatarFullConfig>;
@@ -142,7 +150,11 @@ export const genConfig: GenConfigFunc = (userConfig = {}) => {
142150
response.shirtColor = userConfig.shirtColor || pickRandomFromList(defaultOptions.shirtColor, { avoidList: [_hairOrHatColor] });
143151

144152
// Background color
145-
response.bgColor = userConfig.bgColor || pickRandomFromList(defaultOptions.bgColor, { avoidList: [_hairOrHatColor, response.shirtColor] });
153+
if (userConfig.isGradient) {
154+
response.bgColor = userConfig.bgColor || pickRandomFromList(defaultOptions.gradientBgColor);
155+
} else {
156+
response.bgColor = userConfig.bgColor || pickRandomFromList(defaultOptions.bgColor, { avoidList: [_hairOrHatColor, response.shirtColor] });
157+
}
146158

147159
return response;
148160
};

0 commit comments

Comments
 (0)