Ejemplo n.º 1
0
func main() {
	example := []color.NRGBA{
		{255, 255, 255, 255},
		{255, 255, 0, 255},
		{255, 0, 255, 255},
		{255, 0, 0, 255},
		{0, 255, 255, 255},
		{0, 255, 0, 255},
		{0, 0, 255, 255},
		{0, 0, 0, 255},
		{128, 128, 128, 255},
		{128, 128, 0, 255},
		{128, 0, 128, 255},
		{128, 0, 0, 255},
		{0, 128, 128, 255},
		{0, 128, 0, 255},
		{0, 0, 128, 255},
		{0, 0, 0, 255},
	}

	exampleInterface := make([]color.Color, 0, len(example))
	for _, c := range example {
		exampleInterface = append(exampleInterface, color.Color(c))
	}

	colorshow.DisplaySwatches(exampleInterface)
}
Ejemplo n.º 2
0
func main() {
	var (
		fuzzyness     = flag.Int("fuzz", 5, "Fuzzyness value, lower values take much longer but can potentially get colors that have been missed")
		threshold     = flag.Int("t", 50, "Threshold that colors must differ by")
		display       = flag.Bool("d", false, "Display colors in sdl window")
		terminal      = flag.String("term", "", "Terminal format to output colors as. Currently supported: \"xfce\" \"lilyterm\" \"terminator\"")
		minBrightness = flag.Int("minBright", 100, "Minimum brightness for colors")
		maxBrightness = flag.Int("maxBright", 200, "Maximum brightness for colors")
		debug         = flag.Bool("debug", false, "Show debugging messages")
	)
	flag.Usage = usage
	flag.Parse()
	if *terminal == "" {
		fmt.Println("Must specify terminal format")
		usage()
	}
	if *minBrightness > 255 || *maxBrightness > 255 {
		fmt.Print("Minimum and maximum brightness must be an integer between 0 and 255.\n")
		os.Exit(2)
	}
	if !*debug {
		log.SetOutput(ioutil.Discard)
	}
	img := loadImage(flag.Args()[0])
	w, h := img.Bounds().Max.X, img.Bounds().Max.Y
	colors := make([]color.Color, 0, w*h)
	for x := 0; x < w; x += *fuzzyness {
		for y := 0; y < h; y += *fuzzyness {
			col := color.NRGBAModel.Convert(img.At(x, y))
			colors = append(colors, col)
		}
	}
	distinctColors := getDistinctColors(colors, *threshold, *minBrightness, *maxBrightness)

	count := 0
	for len(distinctColors) < 16 {
		count++
		distinctColors = append(distinctColors, getDistinctColors(colors, *threshold-count, *minBrightness, *maxBrightness)...)
		if count == *threshold {
			fmt.Print("Could not get colors from image with settings specified. Aborting.\n")
			os.Exit(1)
		}
	}

	if len(distinctColors) > 16 {
		distinctColors = distinctColors[:16]
	}

	if *display {
		colorshow.DisplaySwatches(distinctColors)
	}

	switch *terminal {
	case "xfce":
		fmt.Print(printXfce(distinctColors))
	case "lilyterm":
		fmt.Print(printLilyTerm(distinctColors))
	case "terminator":
		fmt.Print(printTerminator(distinctColors))
	case "xterm":
		fmt.Print(printXterm(distinctColors))
	case "konsole":
		fmt.Print(printKonsole(distinctColors))
	default:
		fmt.Print("Did not understand terminal format " + *terminal + "\n")
	}
}
Ejemplo n.º 3
0
func main() {
	terminalSupport := "Terminal format to output colors as. Currently supported: \n"
	for _, t := range terminals {
		terminalSupport += strings.Join([]string{"    ", t.friendlyName, ":", t.flagName, "\n"}, " ")
	}
	var (
		threshold     = flag.Int("t", 50, "Threshold for minimum color difference")
		display       = flag.Bool("d", false, "Display colors in SDL window")
		terminal      = flag.String("term", "default", terminalSupport)
		minBrightness = flag.Int("minBright", 50, "Minimum brightness for colors")
		maxBrightness = flag.Int("maxBright", 200, "Maximum brightness for colors")
		debug         = flag.Bool("debug", false, "Show debugging messages")
	)
	flag.Usage = usage
	flag.Parse()
	if *minBrightness > 255 || *maxBrightness > 255 {
		fmt.Print("Minimum and maximum brightness must be an integer between 0 and 255.\n")
		os.Exit(2)
	}
	if *threshold > 255 {
		fmt.Print("Threshold should be an integer between 0 and 255.\n")
		os.Exit(2)
	}
	if !*debug {
		log.SetOutput(ioutil.Discard)
	}

	// Load the image and create array of colors
	fuzzyness := 5
	img := loadImage(flag.Args()[0])
	w, h := img.Bounds().Max.X, img.Bounds().Max.Y
	colors := make([]color.Color, 0, w*h)
	for x := 0; x < w; x += fuzzyness {
		for y := 0; y < h; y += fuzzyness {
			col := color.NRGBAModel.Convert(img.At(x, y))
			colors = append(colors, col)
		}
	}
	// Get the distinct colors from the array by comparing differences with a threshold
	distinctColors := getDistinctColors(colors, *threshold, *minBrightness, *maxBrightness)

	// Ensure there are 16 colors
	count := 0
	for len(distinctColors) < 16 {
		count++
		distinctColors = append(distinctColors, getDistinctColors(colors, *threshold-count, *minBrightness, *maxBrightness)...)
		if count == *threshold {
			fmt.Print("Could not get colors from image with settings specified. Aborting.\n")
			os.Exit(1)
		}
	}

	if len(distinctColors) > 16 {
		distinctColors = distinctColors[:16]
	}

	if *display {
		colorshow.DisplaySwatches(distinctColors)
	}

	// Output the configuration specified
	terminalMatch := false
	for _, t := range terminals {
		if *terminal == t.flagName {
			fmt.Print(t.output(distinctColors))
			terminalMatch = true
			break
		}
	}
	if !terminalMatch {
		fmt.Printf("Did not recognise format %v. \n", *terminal)
	}
}