mirror of https://github.com/gogits/gogs.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
44 lines
1.2 KiB
44 lines
1.2 KiB
11 years ago
|
// Copyright 2014 The Gogs Authors. All rights reserved.
|
||
|
// Use of this source code is governed by a MIT-style
|
||
|
// license that can be found in the LICENSE file.
|
||
|
|
||
11 years ago
|
package avatar
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
11 years ago
|
"image"
|
||
10 years ago
|
"image/color/palette"
|
||
|
"math/rand"
|
||
11 years ago
|
"time"
|
||
11 years ago
|
|
||
9 years ago
|
"github.com/issue9/identicon"
|
||
11 years ago
|
)
|
||
|
|
||
9 years ago
|
const AVATAR_SIZE = 290
|
||
10 years ago
|
|
||
9 years ago
|
// RandomImage generates and returns a random avatar image unique to input data
|
||
|
// in custom size (height and width).
|
||
9 years ago
|
func RandomImageSize(size int, data []byte) (image.Image, error) {
|
||
10 years ago
|
randExtent := len(palette.WebSafe) - 32
|
||
|
rand.Seed(time.Now().UnixNano())
|
||
|
colorIndex := rand.Intn(randExtent)
|
||
|
backColorIndex := colorIndex - 1
|
||
|
if backColorIndex < 0 {
|
||
|
backColorIndex = randExtent - 1
|
||
|
}
|
||
|
|
||
9 years ago
|
// Define size, background, and forecolor
|
||
|
imgMaker, err := identicon.New(size,
|
||
10 years ago
|
palette.WebSafe[backColorIndex], palette.WebSafe[colorIndex:colorIndex+32]...)
|
||
|
if err != nil {
|
||
9 years ago
|
return nil, fmt.Errorf("identicon.New: %v", err)
|
||
10 years ago
|
}
|
||
|
return imgMaker.Make(data), nil
|
||
|
}
|
||
|
|
||
9 years ago
|
// RandomImage generates and returns a random avatar image unique to input data
|
||
|
// in default size (height and width).
|
||
9 years ago
|
func RandomImage(data []byte) (image.Image, error) {
|
||
9 years ago
|
return RandomImageSize(AVATAR_SIZE, data)
|
||
11 years ago
|
}
|