Skip to content

Commit

Permalink
Merge pull request #3 from tsdtsdtsd/rewrite
Browse files Browse the repository at this point in the history
Rewrite: Road to v1
  • Loading branch information
tsdtsdtsd authored Jan 29, 2023
2 parents 2250813 + 7cd249d commit 265f006
Show file tree
Hide file tree
Showing 24 changed files with 650 additions and 385 deletions.
20 changes: 20 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: Tests and Coverage

on: [push, pull_request]

jobs:
tests:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
with:
fetch-depth: 2
- uses: actions/setup-go@v2
with:
go-version: '1.13'
- name: Run tests
run: go test -v -race ./...
- name: Run coverage
run: go test -v -coverpkg=./... -coverprofile=coverage.txt -covermode=atomic ./...
- name: Upload coverage reports to Codecov
uses: codecov/codecov-action@v3
6 changes: 3 additions & 3 deletions .github/workflows/codeql-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ jobs:

# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
uses: github/codeql-action/init@v1
uses: github/codeql-action/init@v2
with:
languages: ${{ matrix.language }}
# If you wish to specify custom queries, you can do so here or in a config file.
Expand All @@ -53,7 +53,7 @@ jobs:
# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
# If this step fails, then you should remove it and run the build manually (see below)
- name: Autobuild
uses: github/codeql-action/autobuild@v1
uses: github/codeql-action/autobuild@v2

# ℹ️ Command-line programs to run using the OS shell.
# 📚 https://git.io/JvXDl
Expand All @@ -67,4 +67,4 @@ jobs:
# make release

- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v1
uses: github/codeql-action/analyze@v2
20 changes: 0 additions & 20 deletions .travis.yml

This file was deleted.

36 changes: 36 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
GO=go
GOCOVER=$(GO) tool cover
GOTEST=$(GO) test
COVERFILE=coverage.out

GREEN := $(shell tput -Txterm setaf 2)
YELLOW := $(shell tput -Txterm setaf 3)
WHITE := $(shell tput -Txterm setaf 7)
CYAN := $(shell tput -Txterm setaf 6)
RESET := $(shell tput -Txterm sgr0)

.PHONY: test test/cover

all: help

## Test:
test: ## Run all tests
$(GOTEST) -v -race ./...

cover: ## Run tests and open coverage in browser
$(GOTEST) -v -coverpkg=./... -covermode=atomic -coverprofile=$(COVERFILE) ./...
$(GOCOVER) -func=$(COVERFILE)
$(GOCOVER) -html=$(COVERFILE)
rm $(COVERFILE)

## Help:
help: ## Show this help.
@echo ''
@echo 'Usage:'
@echo ' ${YELLOW}make${RESET} ${GREEN}<target>${RESET}'
@echo ''
@echo 'Targets:'
@awk 'BEGIN {FS = ":.*?## "} { \
if (/^[a-zA-Z_-]+:.*?##.*$$/) {printf " ${YELLOW}%-20s${GREEN}%s${RESET}\n", $$1, $$2} \
else if (/^## .*$$/) {printf " ${CYAN}%s${RESET}\n", substr($$1,4)} \
}' $(MAKEFILE_LIST)
104 changes: 52 additions & 52 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,80 +4,80 @@
![Latest Release Version][shields-version-img]
[![Godoc][godoc-image]][godoc-url]
[![Build Status][travis-image]][travis-url]
![Build Status](https://github.com/tsdtsdtsd/identicon/actions/workflows/ci.yml/badge.svg?branch=rewrite)
[![Go Report Card][grc-image]][grc-url]
[![codecov][codecov-image]][codecov-url]
[![CodeQL](https://github.com/tsdtsdtsd/identicon/actions/workflows/codeql-analysis.yml/badge.svg?branch=master)](https://github.com/tsdtsdtsd/identicon/actions/workflows/codeql-analysis.yml)

This Go library helps to generate deterministic [Identicons][identicon-wiki] from strings, like these:
This Go library helps to generate deterministic [Identicons][identicon-wiki] from strings.

![Example Banner](identicon-banner.png "Example Banner")
![Example Banner](_example/bannercreator/identicon-banner.png "Example Banner")

## Installation
---

With module support, you can just import `github.com/tsdtsdtsd/identicon`.
⚠️ I'm working on a complete rewrite for v1.0 ⚠️
It will definitely break the API and most probably also the internal hashing algorithm, so identicons generated after 1.0 will look different than before.

---

## Usage:

Get it:

In other cases you may have to manually:
```sh
go get -u github.com/tsdtsdtsd/identicon
go get github.com/tsdtsdtsd/identicon/v1
```

## Usage example
Create a new identicon:

Basically, you construct a new `Identicon` type with `identicon.New()`, giving it your identification string and some optional `identicon.Options`.
The resulting struct implements `image.Image` and `draw.Image`, so you can use it flexibly.
```go
red := color.RGBA{255, 0, 0, 255}

icon, err := identicon.New(
"[email protected]",
// optional:
identicon.WithGridResolution(7), // default: 5
identicon.WithImageSize(200), // default: 100
identicon.WithBGColor(red), // default: light gray R:240 G:240 B:240 A:255 (#f0f0f0)
)
if err != nil {
log.Fatal(err)
}
```

Just import the library and create a new identicon:
`Identicon` implements Go's `image.Image`, so you can use the result directly to encode it as an image file:

```go
package main

import (
"image/png"
"log"
"os"

"github.com/tsdtsdtsd/identicon"
)

func main() {

ic, err := identicon.New(

// The identicon ID string is mandatory.
// Same string will always result in the same generated identicon.
// Typically this is a username or email address.
"[email protected]",

// You can define custom options or pass nil for defaults
&identicon.Options{
BackgroundColor: identicon.RGB(240, 240, 240),
},
)

if err != nil {
log.Fatal(err)
}

// Now you are free to use identicon `ic` as any other image.Image or draw.Image interface
fi, _ := os.Create("my-file.png")
png.Encode(fi, ic)
file, err := os.Create("identicon-gary.png")
if err != nil {
log.Fatal(err)
}

err = png.Encode(file, icon)
if err != nil {
log.Fatal(err)
}

file.Close()
```

As it also implements Go's `draw.Image`, you can use it to change the output further:

```go
// TODO: example
// draw.Draw(icon, ...
```

### Banner example
## Banner example

You can find another example in the `/example` folder. It contains an application, which generates the above image.
It also helps me to test the algorythm for changes.
You can find another example in the /_example folder. It contains an application, which generates the above image. It also helps me to test the algorythm for changes.

<!-- Markdown link & img dfn's -->
[grc-image]: https://goreportcard.com/badge/github.com/tsdtsdtsd/identicon
[grc-url]: https://goreportcard.com/report/github.com/tsdtsdtsd/identicon
[godoc-image]: https://godoc.org/github.com/tsdtsdtsd/identicon?status.svg
[godoc-url]: https://godoc.org/github.com/tsdtsdtsd/identicon
[travis-image]: https://travis-ci.org/tsdtsdtsd/identicon.svg?branch=master
[travis-url]: https://travis-ci.org/tsdtsdtsd/identicon
[codecov-image]: https://codecov.io/gh/tsdtsdtsd/identicon/branch/master/graph/badge.svg
[codecov-url]: https://codecov.io/gh/tsdtsdtsd/identicon
[godoc-image]: https://pkg.go.dev/badge/github.com/tsdtsdtsd/identicon.svg
[godoc-url]: https://pkg.go.dev/github.com/tsdtsdtsd/identicon
[codecov-image]: https://codecov.io/gh/tsdtsdtsd/identicon/branch/rewrite/graph/badge.svg
[codecov-url]: https://codecov.io/gh/tsdtsdtsd/identicon/tree/rewrite
[shields-version-img]: https://img.shields.io/github/v/release/tsdtsdtsd/identicon
[identicon-wiki]: https://en.wikipedia.org/wiki/Identicon
Binary file added _example/bannercreator/identicon-banner.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ package main

import (
"image"
"image/color"
"image/draw"
"image/png"
"log"
"os"

"github.com/tsdtsdtsd/identicon"
"github.com/tsdtsdtsd/identicon/v1"
)

var genIDs = []string{
Expand Down Expand Up @@ -42,13 +43,13 @@ func main() {

// Create banner and fill background
banner := image.NewRGBA(image.Rect(0, 0, x1, y1))
draw.Draw(banner, image.Rect(0, 0, x1, y1), &image.Uniform{identicon.RGB(255, 255, 255)}, image.ZP, draw.Src)
draw.Draw(banner, image.Rect(0, 0, x1, y1), &image.Uniform{color.RGBA{255, 255, 255, 255}}, image.Point{}, draw.Src)

// Iterate IDs
for i, id := range genIDs {

// Create a new identicon
ic, err := identicon.New(id, nil)
ic, err := identicon.New(id)
if err != nil {
log.Fatal(err)
}
Expand All @@ -65,7 +66,7 @@ func main() {
y1 := y0 + size

// Draw identicon on banner
draw.Draw(banner, image.Rect(x0, y0, x1, y1), ic, image.ZP, draw.Src)
draw.Draw(banner, image.Rect(x0, y0, x1, y1), ic, image.Point{}, draw.Src)
}

// Encode and write banner to file
Expand Down
Binary file removed example/images/12monkeys.png
Binary file not shown.
Binary file removed example/images/Amazatron3000.png
Binary file not shown.
Binary file removed example/images/Stan.Lee.png
Binary file not shown.
Binary file removed example/images/gogopher.png
Binary file not shown.
Binary file removed example/images/m.jackson.png
Binary file not shown.
Binary file removed example/images/notblue.png
Binary file not shown.
Binary file removed example/images/test-string.png
Binary file not shown.
Binary file removed example/images/test.png
Binary file not shown.
Binary file removed example/images/yay-identicons.png
Binary file not shown.
37 changes: 0 additions & 37 deletions example/simple/main.go

This file was deleted.

9 changes: 7 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
module github.com/tsdtsdtsd/identicon
module github.com/tsdtsdtsd/identicon/v1

go 1.11
go 1.13

require (
github.com/stretchr/testify v1.8.1
github.com/tsdtsdtsd/identicon v0.3.2
)
19 changes: 19 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/tsdtsdtsd/identicon v0.3.2 h1:ABkl45kE2SWHfWZaGvcWG47+LBJ143NFxl6MdlmfjhY=
github.com/tsdtsdtsd/identicon v0.3.2/go.mod h1:rxaWtLmj/xwICNxQJD9c0xVkiBgNfZJKv9vNE+FhqG0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Binary file removed identicon-banner.png
Binary file not shown.
Loading

0 comments on commit 265f006

Please sign in to comment.