Example #1
0
// Fit the boundaries of this box to tightly fit the colors within the box.
func (v *vBox) fit() {
	var (
		localMinimumRed   uint8 = math.MaxUint8
		localMinimumGreen uint8 = math.MaxUint8
		localMinimumBlue  uint8 = math.MaxUint8
		localMaximumRed   uint8
		localMaximumGreen uint8
		localMaximumBlue  uint8
		count             uint32
	)
	for i := v.lowerIndex; i <= v.upperIndex; i++ {
		color := v.colors[i]
		count += v.histogram[color]
		r := color.QuantizedRed()
		g := color.QuantizedGreen()
		b := color.QuantizedBlue()
		if r < localMinimumRed {
			localMinimumRed = r
		}
		if r > localMaximumRed {
			localMaximumRed = r
		}
		if g < localMinimumGreen {
			localMinimumGreen = g
		}
		if g > localMaximumGreen {
			localMaximumGreen = g
		}
		if b < localMinimumBlue {
			localMinimumBlue = b
		}
		if b > localMaximumBlue {
			localMaximumBlue = b
		}
	}
	v.minimumRed = uint32(localMinimumRed)
	v.minimumGreen = uint32(localMinimumGreen)
	v.minimumBlue = uint32(localMinimumBlue)
	v.maximumRed = uint32(localMaximumRed)
	v.maximumGreen = uint32(localMaximumGreen)
	v.maximumBlue = uint32(localMaximumBlue)
	v.population = count
}
Example #2
0
// Swatch generates a Swatch for the average color of this box.
func (v *vBox) Swatch() *Swatch {
	var (
		totalRed   uint32
		totalGreen uint32
		totalBlue  uint32
	)
	for _, color := range v.colors[v.lowerIndex : v.upperIndex+1] {
		colorPopulation := v.histogram[color]
		totalRed += colorPopulation * uint32(color.QuantizedRed())
		totalGreen += colorPopulation * uint32(color.QuantizedGreen())
		totalBlue += colorPopulation * uint32(color.QuantizedBlue())
	}
	totalRed = totalRed << (8 - quantizeWordWidth)
	totalGreen = totalGreen << (8 - quantizeWordWidth)
	totalBlue = totalBlue << (8 - quantizeWordWidth)
	averageRed := uint8(roundFloat64(float64(totalRed) / float64(v.population)))
	averageGreen := uint8(roundFloat64(float64(totalGreen) / float64(v.population)))
	averageBlue := uint8(roundFloat64(float64(totalBlue) / float64(v.population)))
	return NewSwatch(color.NRGBA{averageRed, averageGreen, averageBlue, 0xFF}, v.population)
}