Created
October 8, 2017 12:48
-
-
Save hajimehoshi/ba783903bbd1691018e1494ff6c1aefd to your computer and use it in GitHub Desktop.
One-Color
This file contains hidden or 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
package main | |
import ( | |
"image" | |
"image/color" | |
"image/png" | |
"os" | |
) | |
var baseColor = color.RGBA{0x65, 0x73, 0x78, 0xff} | |
func run(in string, out string) error { | |
f, err := os.Open(in) | |
if err != nil { | |
return err | |
} | |
defer f.Close() | |
img, err := png.Decode(f) | |
if err != nil { | |
return err | |
} | |
outimg := image.NewRGBA(img.Bounds()) | |
w, h := img.Bounds().Dx(), img.Bounds().Dy() | |
brf := float64(baseColor.R) / 0xff | |
bgf := float64(baseColor.G) / 0xff | |
bbf := float64(baseColor.B) / 0xff | |
for j := 0; j < h; j++ { | |
for i := 0; i < w; i++ { | |
r, _, _, a := img.At(i, j).RGBA() | |
if a == 0 { | |
continue | |
} | |
rf := float64(r) / 0xffff | |
af := float64(a) / 0xffff | |
c := color.NRGBA{ | |
uint8(((1 - rf) * brf + rf) * 0xff), | |
uint8(((1 - rf) * bgf + rf) * 0xff), | |
uint8(((1 - rf) * bbf + rf) * 0xff), | |
uint8(af * 0xff)} | |
outimg.Set(i, j, c) | |
} | |
} | |
of, err := os.Create(out) | |
if err != nil { | |
return err | |
} | |
defer of.Close() | |
if err := png.Encode(of, outimg); err != nil { | |
return err | |
} | |
return nil | |
} | |
func main() { | |
if err := run(os.Args[1], os.Args[2]); err != nil { | |
panic(err) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment