forked from nicored/avatar
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Stepan Pesternikov
committed
Nov 20, 2018
1 parent
0f7b16e
commit f6bf89b
Showing
6 changed files
with
215 additions
and
50 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,50 @@ | ||
[[dependencies]] | ||
# Gopkg.toml example | ||
# | ||
# Refer to https://golang.github.io/dep/docs/Gopkg.toml.html | ||
# for detailed Gopkg.toml documentation. | ||
# | ||
# required = ["github.com/user/thing/cmd/thing"] | ||
# ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"] | ||
# | ||
# [[constraint]] | ||
# name = "github.com/user/project" | ||
# version = "1.0.0" | ||
# | ||
# [[constraint]] | ||
# name = "github.com/user/project2" | ||
# branch = "dev" | ||
# source = "github.com/myfork/project2" | ||
# | ||
# [[override]] | ||
# name = "github.com/x/y" | ||
# version = "2.4.0" | ||
# | ||
# [prune] | ||
# non-go = false | ||
# go-tests = true | ||
# unused-packages = true | ||
|
||
|
||
[[constraint]] | ||
branch = "master" | ||
name = "github.com/golang/freetype" | ||
|
||
[[dependencies]] | ||
[[constraint]] | ||
name = "github.com/lucasb-eyer/go-colorful" | ||
version = "1.0.0" | ||
|
||
[[constraint]] | ||
branch = "master" | ||
name = "github.com/nfnt/resize" | ||
|
||
[[dependencies]] | ||
[[constraint]] | ||
name = "github.com/stretchr/testify" | ||
version = "v1.1.4" | ||
version = "1.2.2" | ||
|
||
[[dependencies]] | ||
[[constraint]] | ||
branch = "master" | ||
name = "golang.org/x/image" | ||
|
||
[prune] | ||
go-tests = true | ||
unused-packages = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package avatar | ||
|
||
import ( | ||
"github.com/lucasb-eyer/go-colorful" | ||
"image" | ||
"image/draw" | ||
) | ||
|
||
type Color struct { | ||
colorful.Color | ||
} | ||
|
||
// This table contains the "keypoints" of the colorgradient you want to generate. | ||
// The position of each keypoint has to live in the range [0,1] | ||
type GradientTable []struct { | ||
Col Color | ||
Pos float64 | ||
} | ||
|
||
// This is the meat of the gradient computation. It returns a HCL-blend between | ||
// the two colors around `t`. | ||
// Note: It relies heavily on the fact that the gradient keypoints are sorted. | ||
func (gt GradientTable) getInterpolatedColorFor(t float64) Color { | ||
for i := 0; i < len(gt)-1; i++ { | ||
c1 := gt[i] | ||
c2 := gt[i+1] | ||
if c1.Pos <= t && t <= c2.Pos { | ||
// We are in between c1 and c2. Go blend them! | ||
t := (t - c1.Pos) / (c2.Pos - c1.Pos) | ||
return Color{Color: c1.Col.BlendHcl(c2.Col.Color, t).Clamped()} | ||
} | ||
} | ||
|
||
// Nothing found? Means we're at (or past) the last gradient keypoint. | ||
return gt[len(gt)-1].Col | ||
} | ||
|
||
// This is a very nice thing Golang forces you to do! | ||
// It is necessary so that we can write out the literal of the colortable below. | ||
func MustParseHex(s string) Color { | ||
c, err := colorful.Hex(s) | ||
if err != nil { | ||
panic("MustParseHex: " + err.Error()) | ||
} | ||
return Color{Color: c} | ||
} | ||
|
||
func newGradientImage(height, width int, gradientTable GradientTable) *image.RGBA { | ||
img := image.NewRGBA(image.Rect(0, 0, width, height)) | ||
|
||
for y := height - 1; y >= 0; y-- { | ||
c := gradientTable.getInterpolatedColorFor(float64(y) / float64(height)) | ||
draw.Draw(img, image.Rect(0, y, width, y+1), &image.Uniform{C: c}, image.ZP, draw.Src) | ||
} | ||
|
||
return img | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.