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.

avatar.go 1.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package avatar
  5. import (
  6. "fmt"
  7. "image"
  8. "image/color/palette"
  9. "math/rand"
  10. "time"
  11. "github.com/issue9/identicon"
  12. )
  13. const _RANDOM_AVATAR_SIZE = 200
  14. func RandomImageSize(size int, data []byte) (image.Image, error) {
  15. randExtent := len(palette.WebSafe) - 32
  16. rand.Seed(time.Now().UnixNano())
  17. colorIndex := rand.Intn(randExtent)
  18. backColorIndex := colorIndex - 1
  19. if backColorIndex < 0 {
  20. backColorIndex = randExtent - 1
  21. }
  22. // Define size, background, and forecolor
  23. imgMaker, err := identicon.New(size,
  24. palette.WebSafe[backColorIndex], palette.WebSafe[colorIndex:colorIndex+32]...)
  25. if err != nil {
  26. return nil, fmt.Errorf("identicon.New: %v", err)
  27. }
  28. return imgMaker.Make(data), nil
  29. }
  30. // RandomImage generates and returns a random avatar image.
  31. // The data should normally be the []byte type of email address.
  32. func RandomImage(data []byte) (image.Image, error) {
  33. return RandomImageSize(_RANDOM_AVATAR_SIZE, data)
  34. }