Skip to content
Open
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
6 changes: 4 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { pick } from './utils/array'
import { createBackground, createBlackout, createSvg } from './utils/svg'
import { seedrandom } from './utils/random'
import { generateSeedRandom } from './utils/random'
import { avatarColors as aColors, backgroundColors as bColors } from './palette'
import { Shape, faces, ears, muzzles, eyes, brows, patterns, hairs, emptyShape } from './shapes'
import { convertIfRgbaColor } from './utils/colors'

export type AvatarOptions = {
size?: number;
Expand All @@ -22,7 +23,8 @@ const avatar = (
round = true,
}: AvatarOptions = {},
): string => {
const random = seedrandom(seed)
avatarColors = convertIfRgbaColor(avatarColors)
const random = generateSeedRandom(seed)
const backgroundColor = pick(backgroundColors, random())
const avatarColor = pick(avatarColors, random())
const optional = (shapes: Shape[]) =>
Expand Down
5 changes: 5 additions & 0 deletions src/palette/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,8 @@ export const avatarColors = [
'#99c9bd',
'#50c8c6',
]

export const avatarColorsRGB = [
'rgba(255, 99, 71, 0.6)',
'rgba(122, 99, 71, 0.6)',
]
25 changes: 25 additions & 0 deletions src/utils/colors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,28 @@ const rgb2Hex = (rgb: number[]) =>

const clamp = (value: number, min: number, max: number) =>
Math.min(max, Math.max(min, value))

export const rgba2hex = (orig: string) => {
let a:string
const rgb = orig.replace(/\s/g, '').match(/^rgba?\((\d+),(\d+),(\d+),?([^,\s)]+)?/i),
alpha = (rgb && rgb[4] || '').trim(),
hex = rgb ?
(parseInt(rgb[1]) | 1 << 8).toString(16).slice(1) +
(parseInt(rgb[2]) | 1 << 8).toString(16).slice(1) +
(parseInt(rgb[3]) | 1 << 8).toString(16).slice(1) : orig

if (alpha !== '') {
a = alpha
} else {
a = '01'
}
// multiply before convert to HEX
a = ((parseInt(a) * 255) | 1 << 8).toString(16).slice(1)
const newHex = hex
return `#${newHex}`
}

const testIfRgba = (color: string)=> /^rgba?\((\d+),\s*(\d+),\s*(\d+)(?:,\s*(\d+(?:\.\d+)?))?\)$/.test(color)

export const convertIfRgbaColor = (colors: string[]) =>
colors.map((color: string)=>testIfRgba(color) ? rgba2hex(color) : color)
11 changes: 9 additions & 2 deletions src/utils/random.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
export const seedrandom = (seed: string) => {
export function* seedrandom (seed: string) {
let value = hash(seed)
const nextValue = () => value = xorshift32(value)
return () => nextValue()
while(true){
yield nextValue()
}
}

export function generateSeedRandom (seed: string){
const seedRandomGen = seedrandom(seed)
return ()=>seedRandomGen.next().value ?? 0
}

const xorshift32 = (value: number) =>
Expand Down