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

  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. // AvatarSize returns avatar's size
  14. const AvatarSize = 290
  15. // RandomImageSize generates and returns a random avatar image unique to input data
  16. // in custom size (height and width).
  17. func RandomImageSize(size int, data []byte) (image.Image, error) {
  18. randExtent := len(palette.WebSafe) - 32
  19. rand.Seed(time.Now().UnixNano())
  20. colorIndex := rand.Intn(randExtent)
  21. backColorIndex := colorIndex - 1
  22. if backColorIndex < 0 {
  23. backColorIndex = randExtent - 1
  24. }
  25. // Define size, background, and forecolor
  26. imgMaker, err := identicon.New(size,
  27. palette.WebSafe[backColorIndex], palette.WebSafe[colorIndex:colorIndex+32]...)
  28. if err != nil {
  29. return nil, fmt.Errorf("identicon.New: %v", err)
  30. }
  31. return imgMaker.Make(data), nil
  32. }
  33. // RandomImage generates and returns a random avatar image unique to input data
  34. // in default size (height and width).
  35. func RandomImage(data []byte) (image.Image, error) {
  36. return RandomImageSize(AvatarSize, data)
  37. }