configurable avatar colors

This commit is contained in:
Ted Unangst 2019-12-09 21:20:35 -05:00
parent ff9d4336db
commit 150af9cc7c
7 changed files with 77 additions and 27 deletions

View File

@ -36,6 +36,9 @@ func adminscreen() {
smcup := esc + "[?1049h" smcup := esc + "[?1049h"
rmcup := esc + "[?1049l" rmcup := esc + "[?1049l"
var avatarColors string
getconfig("avatarcolors", &avatarColors)
messages := []*struct { messages := []*struct {
name string name string
label string label string
@ -56,6 +59,11 @@ func adminscreen() {
label: "login", label: "login",
text: string(loginMsg), text: string(loginMsg),
}, },
{
name: "avatarcolors",
label: "avatar colors (4 RGBA hex numbers)",
text: string(avatarColors),
},
} }
cursel := 0 cursel := 0
@ -239,7 +247,7 @@ func adminscreen() {
stdout.Flush() stdout.Flush()
} }
editing = false editing = false
updateconfig(m.name, m.text) setconfig(m.name, m.text)
hidecursor() hidecursor()
drawscreen() drawscreen()
} }

View File

@ -16,13 +16,52 @@
package main package main
import ( import (
"bufio"
"bytes" "bytes"
"crypto/sha512" "crypto/sha512"
"image" "image"
"image/png" "image/png"
"log"
"strconv"
"strings"
) )
func avatar(name string) []byte { var avatarcolors = [4][4]byte{
{16, 0, 48, 255},
{48, 0, 96, 255},
{72, 0, 144, 255},
{96, 0, 192, 255},
}
func loadAvatarColors() {
var colors string
getconfig("avatarcolors", &colors)
if colors == "" {
return
}
r := bufio.NewReader(strings.NewReader(colors))
for i := 0; i < 4; i++ {
l, _ := r.ReadString(' ')
for l == " " {
l, _ = r.ReadString(' ')
}
l = strings.Trim(l, "# \n")
if len(l) == 6 {
l = l + "ff"
}
c, err := strconv.ParseUint(l, 16, 32)
if err != nil {
log.Printf("error reading avatar color %d: %s", i, err)
continue
}
avatarcolors[i][0] = byte(c >> 24 & 0xff)
avatarcolors[i][1] = byte(c >> 16 & 0xff)
avatarcolors[i][2] = byte(c >> 8 & 0xff)
avatarcolors[i][3] = byte(c >> 0 & 0xff)
}
}
func genAvatar(name string) []byte {
h := sha512.New() h := sha512.New()
h.Write([]byte(name)) h.Write([]byte(name))
s := h.Sum(nil) s := h.Sum(nil)
@ -33,25 +72,25 @@ func avatar(name string) []byte {
xx := i/16*16 + j/16 xx := i/16*16 + j/16
x := s[xx] x := s[xx]
if x < 64 { if x < 64 {
img.Pix[p+0] = 16 img.Pix[p+0] = avatarcolors[0][0]
img.Pix[p+1] = 0 img.Pix[p+1] = avatarcolors[0][1]
img.Pix[p+2] = 48 img.Pix[p+2] = avatarcolors[0][2]
img.Pix[p+3] = 255 img.Pix[p+3] = avatarcolors[0][3]
} else if x < 128 { } else if x < 128 {
img.Pix[p+0] = 48 img.Pix[p+0] = avatarcolors[1][0]
img.Pix[p+1] = 0 img.Pix[p+1] = avatarcolors[1][1]
img.Pix[p+2] = 96 img.Pix[p+2] = avatarcolors[1][2]
img.Pix[p+3] = 255 img.Pix[p+3] = avatarcolors[1][3]
} else if x < 192 { } else if x < 192 {
img.Pix[p+0] = 72 img.Pix[p+0] = avatarcolors[2][0]
img.Pix[p+1] = 0 img.Pix[p+1] = avatarcolors[2][1]
img.Pix[p+2] = 144 img.Pix[p+2] = avatarcolors[2][2]
img.Pix[p+3] = 255 img.Pix[p+3] = avatarcolors[2][3]
} else { } else {
img.Pix[p+0] = 96 img.Pix[p+0] = avatarcolors[3][0]
img.Pix[p+1] = 0 img.Pix[p+1] = avatarcolors[3][1]
img.Pix[p+2] = 192 img.Pix[p+2] = avatarcolors[3][2]
img.Pix[p+3] = 255 img.Pix[p+3] = avatarcolors[3][3]
} }
} }
} }

View File

@ -2,6 +2,8 @@ changelog
-- next -- next
+ Configurable avatar colors.
+ Optional pleroma color scheme for the home sick... + Optional pleroma color scheme for the home sick...
+ Rebalance colors slightly. Looks a little fresher now? + Rebalance colors slightly. Looks a little fresher now?

View File

@ -100,6 +100,8 @@ Displayed on the home page.
Displayed on the about page. Displayed on the about page.
.It login .It login
Displayed about the login form. Displayed about the login form.
.It avatar colors
Four 32-bit hex colors (RGBA).
.El .El
.Pp .Pp
.Ss User Admin .Ss User Admin

View File

@ -262,9 +262,9 @@ func main() {
} }
switch args[1] { switch args[1] {
case "on": case "on":
updateconfig("debug", 1) setconfig("debug", 1)
case "off": case "off":
updateconfig("debug", 0) setconfig("debug", 0)
default: default:
log.Fatal("argument must be on or off") log.Fatal("argument must be on or off")
} }

View File

@ -391,16 +391,11 @@ func getconfig(key string, value interface{}) error {
func setconfig(key string, val interface{}) error { func setconfig(key string, val interface{}) error {
db := opendatabase() db := opendatabase()
db.Exec("delete from config where key = ?", key)
_, err := db.Exec("insert into config (key, value) values (?, ?)", key, val) _, err := db.Exec("insert into config (key, value) values (?, ?)", key, val)
return err return err
} }
func updateconfig(key string, val interface{}) error {
db := opendatabase()
_, err := db.Exec("update config set value = ? where key = ?", val, key)
return err
}
func openListener() (net.Listener, error) { func openListener() (net.Listener, error) {
var listenAddr string var listenAddr string
err := getconfig("listenaddr", &listenAddr) err := getconfig("listenaddr", &listenAddr)

6
web.go
View File

@ -1988,8 +1988,11 @@ func somedays() string {
} }
func avatate(w http.ResponseWriter, r *http.Request) { func avatate(w http.ResponseWriter, r *http.Request) {
if debugMode {
loadAvatarColors()
}
n := r.FormValue("a") n := r.FormValue("a")
a := avatar(n) a := genAvatar(n)
w.Header().Set("Cache-Control", "max-age="+somedays()) w.Header().Set("Cache-Control", "max-age="+somedays())
w.Write(a) w.Write(a)
} }
@ -2261,6 +2264,7 @@ func serve() {
for _, s := range assets { for _, s := range assets {
savedassetparams[s] = getassetparam(s) savedassetparams[s] = getassetparam(s)
} }
loadAvatarColors()
} }
for _, h := range preservehooks { for _, h := range preservehooks {