diff --git a/.gitignore b/.gitignore deleted file mode 100644 index 28b8fc3..0000000 --- a/.gitignore +++ /dev/null @@ -1,34 +0,0 @@ -hashcolor -*~ -*.swp -*.swo - -# Object files -*.o -*.ko -*.obj -*.elf - -# Precompiled Headers -*.gch -*.pch - -# Libraries -*.lib -*.a -*.la -*.lo - -# Shared objects (inc. Windows DLLs) -*.dll -*.so -*.so.* -*.dylib - -# Executables -*.exe -*.out -*.app -*.i*86 -*.x86_64 -*.hex diff --git a/Makefile b/Makefile deleted file mode 100644 index 4cfda69..0000000 --- a/Makefile +++ /dev/null @@ -1,11 +0,0 @@ -# $Id$ - -PROG=hashcolor -BINDIR=/usr/local/bin -NOMAN= - -#afterinstall: -patch: - patch `which uxterm` uxterm.patch - -.include diff --git a/README.md b/README.md index 3e9c1b9..0adb884 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,7 @@ # hashcolor + Simple color randomizer for uxterm -Build and install with `pmake' on linux +## install -Patch uxterm with `pmake patch' + go get -u github.com/dim13/hashcolor/cmd/hashcolor diff --git a/cmd/hashcolor/main.go b/cmd/hashcolor/main.go new file mode 100644 index 0000000..a015cec --- /dev/null +++ b/cmd/hashcolor/main.go @@ -0,0 +1,21 @@ +package main + +import ( + "fmt" + "image/color" + "os" + "strings" + + "github.com/dim13/hashcolor" +) + +func hex(c color.Color) string { + rgba := color.RGBAModel.Convert(c).(color.RGBA) + return fmt.Sprintf("#%.2x%.2x%.2x", rgba.R, rgba.G, rgba.B) +} + +func main() { + c := hashcolor.New(strings.Join(os.Args[1:], " ")) + t, s := hashcolor.Tint(c), hashcolor.Shade(c) + fmt.Println("-fg", hex(t), "-bg", hex(s)) +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..ea3d559 --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module github.com/dim13/hashcolor + +go 1.12 + +require github.com/dim13/crc24 v0.0.0-20190308110643-af7201913116 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..adb68c7 --- /dev/null +++ b/go.sum @@ -0,0 +1,2 @@ +github.com/dim13/crc24 v0.0.0-20190308110643-af7201913116 h1:Yg0Qk0ozxBmyHXaFiyAokM5Gf/uCFm7Vy+ukpFPiFVQ= +github.com/dim13/crc24 v0.0.0-20190308110643-af7201913116/go.mod h1:TejqRSRwQ36N6MSUaGN8WipY4g3bgf5HeEi9fApCxG0= diff --git a/hashcolor.c b/hashcolor.c deleted file mode 100644 index 08dec64..0000000 --- a/hashcolor.c +++ /dev/null @@ -1,89 +0,0 @@ -/* $Id$ */ -/* - * Copyright (c) 2015 Dimitri Sokolyuk - * - * Permission to use, copy, modify, and distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#include -#include - -#define CRC24_INIT 0x0B704CEL -#define CRC24_POLY 0x1864CFBL - -long -crc24(char *s) -{ - long crc; - int i; - - for (crc = CRC24_INIT; *s; s++) { - crc ^= *s << 0x10; - for (i = 0; i < 8; i++) { - crc <<= 1; - if (crc & 0x1000000) - crc ^= CRC24_POLY; - } - } - - return crc; -} - -long -shade(long c) -{ - unsigned char r = c >> 0x10; - unsigned char g = c >> 0x08; - unsigned char b = c; - - r >>= 2; - g >>= 2; - b >>= 2; - - return (r << 0x10) | (g << 0x8) | b; -} - -long -tint(long c) -{ - unsigned char r = c >> 0x10; - unsigned char g = c >> 0x08; - unsigned char b = c; - - r += (0xFF - r) >> 1; - g += (0xFF - g) >> 1; - b += (0xFF - b) >> 1; - - return (r << 0x10) | (g << 0x8) | b; -} - -#if defined (__linux__) -#define strlcpy(d,s,l) (strncpy(d,s,(l)-strlen(d)-1),(d)[(l)-1]='\0') -#define strlcat(d,s,l) strncat(d,s,(l)-strlen(d)-1) -#endif - -int -main(int argc, char **argv) -{ - char arg[256] = {}; - long color; - - while (*++argv) - strlcat(arg, *argv, sizeof(arg)); - - color = crc24(arg); - - printf("-fg #%.6lx -bg #%.6lx\n", tint(color), shade(color)); - - return 0; -} diff --git a/hashcolor.go b/hashcolor.go new file mode 100644 index 0000000..346817e --- /dev/null +++ b/hashcolor.go @@ -0,0 +1,36 @@ +package hashcolor + +import ( + "image/color" + + "github.com/dim13/crc24" +) + +// New color from hashed string +func New(s string) color.RGBA { + c := crc24.Sum([]byte(s)) + return color.RGBA{ + R: uint8(c >> 0x10), + G: uint8(c >> 0x08), + B: uint8(c), + A: 0xff, + } +} + +// Tint color +func Tint(c color.Color) color.RGBA { + rgba := color.RGBAModel.Convert(c).(color.RGBA) + rgba.R += (0xff - rgba.R) >> 1 + rgba.G += (0xff - rgba.G) >> 1 + rgba.B += (0xff - rgba.B) >> 1 + return rgba +} + +// Shade color +func Shade(c color.Color) color.RGBA { + rgba := color.RGBAModel.Convert(c).(color.RGBA) + rgba.R >>= 2 + rgba.G >>= 2 + rgba.B >>= 2 + return rgba +}