-
Notifications
You must be signed in to change notification settings - Fork 1
/
colors.go
154 lines (140 loc) · 4.04 KB
/
colors.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
WebChunk, web server for block game maps
Copyright (C) 2022 Maxim Zhuchkov
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
Contact me via mail: [email protected] or Discord: MaX#6717
*/
package main
import (
"bytes"
"encoding/gob"
"fmt"
"image/color"
"log"
"net/http"
"os"
"strconv"
"github.com/maxsupermanhd/go-vmc/v764/level/block"
)
func hexColor(c color.Color) string {
rgba := color.RGBAModel.Convert(c).(color.RGBA)
return fmt.Sprintf("%.2x%.2x%.2x%.2x", rgba.R, rgba.G, rgba.B, rgba.A)
}
func colorsHandlerGET(w http.ResponseWriter, r *http.Request) {
offsets := r.URL.Query().Get("o")
counts := r.URL.Query().Get("c")
var offset, count int
var err error
if offsets == "" {
offset = 0
} else {
offset, err = strconv.Atoi(offsets)
if err != nil {
plainmsg(w, r, plainmsgColorRed, "Failed to parse offset: "+err.Error())
return
}
}
if counts == "" {
count = 1000
} else {
count, err = strconv.Atoi(counts)
if err != nil {
plainmsg(w, r, plainmsgColorRed, "Failed to parse count: "+err.Error())
return
}
}
type BlockColor struct {
BlockID string
BlockDescription string
Color string
}
c := map[int]BlockColor{}
for i, b := range block.StateList {
if i < offset {
continue
}
if i > offset+count {
continue
}
s := BlockColor{
b.ID(),
fmt.Sprintf("%##v", b),
hexColor(colors[uint32(i)])}
c[i] = s
}
templateRespond("colors", w, r, map[string]interface{}{"Colors": c, "Offset": offset, "Count": count})
}
func ParseHexColor(s string) (c color.RGBA64, err error) {
t := color.RGBA{}
_, err = fmt.Sscanf(s, "#%02x%02x%02x%02x", &t.R, &t.G, &t.B, &t.A)
c.R = uint16(t.R) * 256
c.G = uint16(t.G) * 256
c.B = uint16(t.B) * 256
c.A = uint16(t.A) * 256
return
}
func colorsHandlerPOST(w http.ResponseWriter, r *http.Request) {
err := r.ParseForm()
if err != nil {
plainmsg(w, r, plainmsgColorRed, "Error parsing form data: "+err.Error())
return
}
colorids := r.PostFormValue("colorid")
if colorids == "" {
plainmsg(w, r, plainmsgColorRed, "No colorid in request")
return
}
colorid, err := strconv.Atoi(colorids)
if err != nil {
plainmsg(w, r, plainmsgColorRed, "Failed to parse colorid: "+err.Error())
return
}
if colorid < 0 || colorid > len(colors) {
plainmsg(w, r, plainmsgColorRed, "Bad colorid in request")
return
}
colorvalue := r.PostFormValue("colorvalue")
if colorvalue == "" {
plainmsg(w, r, plainmsgColorRed, "No colorvalue in request")
return
}
newColor, err := ParseHexColor(colorvalue)
if err != nil {
plainmsg(w, r, plainmsgColorRed, "Failed to parse colorvalue")
return
}
colors[uint32(colorid)] = newColor
plainmsg(w, r, plainmsgColorGreen, fmt.Sprint("Color ", colorid, " was changed to ", newColor))
colorsHandlerGET(w, r)
}
var colors []color.RGBA64
func colorsSaveHandler(w http.ResponseWriter, r *http.Request) {
f, err := os.Create(cfg.GetDSString("./colors.gob", "colors_path"))
if err != nil {
plainmsg(w, r, plainmsgColorRed, "Error saving color palette to disk: "+err.Error())
return
}
defer f.Close()
if err := gob.NewEncoder(f).Encode(colors); err != nil {
plainmsg(w, r, plainmsgColorRed, "Error saving color palette to disk: "+err.Error())
return
}
plainmsg(w, r, plainmsgColorGreen, "Color palette saved to disk.")
}
func loadColors(path string) error {
log.Println("Loading color palette...")
b, err := os.ReadFile(path)
if err != nil {
return err
}
return gob.NewDecoder(bytes.NewReader(b)).Decode(&colors)
}