commit 32d9ef18ebe5932f507591085e60d8ab8c1111f1 Author: Manuel Date: Wed Jul 19 22:00:00 2023 +0200 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3f643a0 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/humanshader \ No newline at end of file diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100644 index 0000000..be5b931 --- /dev/null +++ b/LICENSE.txt @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2023 SunRed + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..70a1d25 --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# Human Shader Validator + +Validator for [Human Shader](https://humanshader.com/) \ No newline at end of file diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..8a1b0d6 --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module git.snrd.eu/sunred/humanshader + +go 1.20 + +require () \ No newline at end of file diff --git a/hsv.go b/hsv.go new file mode 100644 index 0000000..6658283 --- /dev/null +++ b/hsv.go @@ -0,0 +1,33 @@ +package main + +import ( + "bufio" + "fmt" + hsv "git.snrd.eu/sunred/humanshader/validator" + "os" + "strconv" +) + +func main() { + args := os.Args[1:] + var pxX int + var pxY int + if len(args) == 2 { + pxX, _ = strconv.Atoi(args[0]) + pxY, _ = strconv.Atoi(args[1]) + } else { + fmt.Print("Pixel x coordinate: ") + input := bufio.NewScanner(os.Stdin) + input.Scan() + pxX, _ = strconv.Atoi(input.Text()) + fmt.Print("Pixel y coordinate: ") + input = bufio.NewScanner(os.Stdin) + input.Scan() + pxY, _ = strconv.Atoi(input.Text()) + } + + val := hsv.NewValidator(true) + val.Calculate(pxX, pxY) + r, g, b := val.GetColor() + fmt.Printf("R: %d\nG: %d\nB: %d\nHEX: #%02X%02X%02X\n", r, g, b, r, g, b) +} diff --git a/validator/validator.go b/validator/validator.go new file mode 100644 index 0000000..f55b00c --- /dev/null +++ b/validator/validator.go @@ -0,0 +1,171 @@ +package validator + +import ( + "fmt" +) + +type Validator struct { + printMode bool + r int + g int + b int +} + +func NewValidator(print bool) Validator { + return Validator{print, 0, 0, 0} +} + +func (val *Validator) Calculate(x int, y int) { + val.print("x = %d", x) + val.print("y = %d", y) + var u int = x - 36 + val.print("u = %d - 36 = %d", x, u) + var v int = 18 - y + val.print("v = 18 - %d = %d", y, v) + var h int = u*u + v*v + val.print("h = %d² + %d² = %d + %d = %d", u, v, u*u, v*v, h) + if h < 200 { + val.print("⇒ h < 200 → %d < 200", h) + val.print("=> Continuing to section B") + val.sectionB(u, v, h) + } else if v < 0 { + val.print("⇒ h < 200 → %d ≮ 200", h) + val.print("⇒ v < 0 → %d < 0", v) + val.print("=> Continuing to section C") + val.sectionC(u, v, h) + } else { + val.print("⇒ h < 200 → %d ≮ 200", h) + val.print("⇒ v < 0 → %d ≮ 0", v) + val.print("=> Continuing to section D") + val.sectionD(x, y) + } +} + +func (val *Validator) sectionB(u int, v int, h int) { + val.r = 420 + val.print("R = %d", val.r) + val.b = 520 + val.print("B = %d", val.b) + var t int = 5000 + 8*h + val.print("t = 5000 + 8 ⋅ %d = 5000 + %d = %d", h, 8*h, t) + + var p int = val.mod(t*u, 2) + val.print("p = (%d ⋅ %d) | 2 = %d | 2 = %d", t, u, t*u, p) + var q int = val.mod(t*v, 2) + val.print("q = (%d ⋅ %d) | 2 = %d | 2 = %d", t, v, t*v, q) + var s int = 2 * q + val.print("s = 2 ⋅ %d = %d", q, s) + var w int = val.mod(1000+p-s, 2) + 8 + val.print("w = (1000 + %d - %d) | 2 + 8 = (1000 + %d) | 2 + 8 = %d | 2 + 8 = %d + 8 = %d", p, s, p-s, 1000+p-s, val.mod(1000+p-s, 2), w) + if w > 0 { + val.print("⇒ w > 0 → %d > 0", w) + val.r = val.r + w*w + val.print(" R → R + %d² → R + %d ⇒ R = %d", w, w*w, val.r) + } + var o int = s + 2200 + val.print("o = %d + 2200", s) + val.r = val.mod(val.r*o, 4) + val.print("R → R ⋅ %d | 4 ⇒ R = %d", o, val.r) + val.b = val.mod(val.b*o, 4) + val.print("B → B ⋅ %d | 4 ⇒ B = %d", o, val.b) + if p > -q { + val.print("⇒ p > -q → %d > -%d", p, q) + w = val.mod(p+q, 1) + val.print(" w = (%d + %q) | 1 = %d | 1 = %d", p, q, p+q, w) + val.r = val.r + w + val.print(" R → R + %d ⇒ R = %d", w, val.r) + val.b = val.b + w + val.print(" B → B + %d ⇒ B = %d", w, val.b) + } + val.print("=> Continuing to section E") + val.sectionE() +} + +func (val *Validator) sectionC(u int, v int, h int) { + val.r = 150 + 2*v + val.print("R = 150 + 2 ⋅ %d = 150 + %d = %d", v, 2*v, val.r) + val.b = 50 + val.print("B = %d", val.b) + var p int = h + 8*v*v + val.print("p = %d + 8 ⋅ %d² = %d + 8 ⋅ %d = %d + %d = %d", h, v, h, v*v, h, 8*v*v, p) + var c int = 240*(-v) - p + val.print("c = 240 ⋅ (-%d) - %d = %d - %d = %d", v, p, 240*(-v), p, c) + if c > 1200 { + val.print("⇒ c > 1200 → %d > 1200", c) + var o int = val.mod(6*c, 1) + val.print(" o = (6 ⋅ %d) | 1 = %d | 1 = %d", c, 6*c, o) + o = c * (1500 - o) + val.print(" o → %d ⋅ (1500 - o) = %d", c, o) + o = val.mod(o, 2) - 8360 + val.print(" o → o | 2 - 8360 = %d", o) + val.r = val.mod(val.r*o, 3) + val.print(" R → R ⋅ %d | 3 ⇒ R = %d", o, val.r) + val.b = val.mod(val.b*o, 3) + val.print(" B → B ⋅ %d | 3 ⇒ B = %d", o, val.b) + } + var r int = c + u*v + val.print("r = %d + %d ⋅ %d = %d + %d = %d", c, u, v, c, u*v, r) + var d int = 3200 - h - 2*r + val.print("d = 3200 - %d - 2 ⋅ %d = %d + %d = %d", h, r, 3200-h, 2*r, d) + if d > 0 { + val.print("⇒ c > 0 → %d > 0", d) + val.r = val.r + d + val.print(" R → R + %d ⇒ R = %d", d, val.r) + } + val.print("=> Continuing to section E") + val.sectionE() +} + +func (val *Validator) sectionD(x int, y int) { + var c int = x + 4*y + val.print("c = %d + 4 ⋅ %d", x, y) + val.r = 132 + c + val.print("R = 132 + %d = %d", c, val.r) + val.b = 192 + c + val.print("R = 192 + %d = %d", c, val.b) + val.print("=> Continuing to section E") + val.sectionE() +} + +func (val *Validator) sectionE() { + if val.r > 255 { + val.r = 255 + val.print("⇒ R > 255 → R = %d", val.r) + } + if val.b > 255 { + val.b = 255 + val.print("⇒ B > 255 → B = %d", val.b) + } + val.g = val.mod(7*val.r+3*val.b, 1) + val.print("G = (7 + %d + 3 ⋅ %d) | 1 = (%d + %d) | 1 = %d | 1 = %d", val.r, val.b, 7+val.r, 3*val.b, 7*val.r+3*val.b, val.g) +} + +func (val *Validator) mod(left int, right int) int { + l := left + if left < 0 { + l = -left + } + for i := 0; i < right-1; i++ { + l /= 10 + } + r := 0 + if l%10 < 5 { + r = l / 10 + } else { + r = l/10 + 1 + } + if left < 0 { + r = -r + } + return r +} + +func (val *Validator) GetColor() (int, int, int) { + return val.r, val.g, val.b +} + +func (val *Validator) print(str string, args... any) { + if val.printMode { + fmt.Printf(str + "\n", args...) + } +}