Example #1
0
File: cmd.go Project: hawx/quantise
func main() {
	flag.Parse()

	img, data := utils.ReadStdin()

	s := quantise.LEAST
	if *strategy == "MOST" {
		s = quantise.MOST
	}

	img = quantise.Quantise(img, quantise.OctreeQuantiser{
		Depth:    uint8(*depth),
		Size:     *size,
		Strategy: s,
	})

	utils.WriteStdout(img, data)
}
Example #2
0
// GetPalette finds a palette of the dominant colours in an image. Various
// parameters, such as the number of dominant colours to return, are given by
// the Options.
func GetPalette(img image.Image, opts *Options) Palette {
	minDistance := MIN_DISTANCE
	minSaturation := MIN_SATURATION
	minProminence := MIN_PROMINENCE
	maxColors := MAX_COLORS
	nQuantized := N_QUANTIZED

	if opts != nil {
		if opts.MinDistance != 0 {
			minDistance = opts.MinDistance
		}
		if opts.MaxColors != 0 {
			maxColors = opts.MaxColors
		}
		if opts.MinProminence != 0 {
			minProminence = opts.MinProminence
		}
		if opts.MinSaturation != 0 {
			minSaturation = opts.MinSaturation
		}
		if opts.NQuantized != 0 {
			nQuantized = opts.NQuantized
		}
	}

	q := quantise.OctreeQuantiser{
		Depth:    6,
		Size:     nQuantized,
		Strategy: quantise.LEAST,
	}

	img = quantise.Quantise(img, q)

	sortedCols := countColors(img)

	colors, toCanonical := aggregate(sortedCols, minDistance)

	colors, bgColor := detectBackground(img, colors, toCanonical)

	// keep any color which meets the minimum saturation
	satColors := ColorProminences{}
	for _, c := range colors {
		if meetsMinSaturation(c.Value, minSaturation) {
			satColors = append(satColors, c)
		}
	}

	if bgColor != nil && !meetsMinSaturation(bgColor, minSaturation) {
		bgColor = nil
	} else {
		if len(satColors) > 0 {
			colors = satColors
		} else {
			// keep at least one color
			colors = colors[:1]
		}
	}

	// keep any color within 10% of the majority color
	finalColors := []ColorProminence{}
	for _, c := range colors {
		if c.Prominence >= colors[0].Prominence*minProminence {
			finalColors = append(finalColors, c)
		}
	}

	return Palette{finalColors[:maxColors], bgColor}
}