pixclient/pixclient.go

51 lines
1 KiB
Go
Raw Normal View History

2017-05-26 13:33:18 +00:00
package pixclient
import (
"net"
"strconv"
)
type Client struct {
connection net.Conn
}
func Init(addr net.IP, port int) *Client {
var client Client
connection, err := net.Dial("tcp", addr.String()+":"+strconv.Itoa(port))
client.connection = connection
check(err)
return &client
}
func (Client *Client) WritePixel(x, y int, r, g, b, a byte) {
var bytes []byte
bytes = append(appendTwoDigitHex(appendTwoDigitHex(appendTwoDigitHex(appendTwoDigitHex(append(append(append(append(append(
bytes, []byte("PX ")...),
[]byte(strconv.FormatInt(int64(x), 10))...),
[]byte(" ")...),
[]byte(strconv.FormatInt(int64(y), 10))...),
[]byte(" ")...),
r),
g),
b),
a),
[]byte("\n")...)
Client.connection.Write(bytes)
//
//Client.connection.Write([]byte(
// fmt.Sprintf("PX %d %d %X\n", x, y, []byte{r, g, b, a})))
}
func appendTwoDigitHex(bytes []byte, val byte) []byte {
if val < 0x10 {
bytes = append(bytes, []byte("0")...)
}
return strconv.AppendInt(bytes, int64(val), 16)
}
func check(e error) {
if e != nil {
panic(e)
}
}