Example #1
0
func ColorIsGood(c color.Color) bool {
	r, _, _, _ := c.RGBA()
	if r > 30000 {
		return true
	}
	return false
}
Example #2
0
// The 'over' operator for blending colors. Color a will be painted
// over color b.
func over(a, b color.Color) color.Color {
	const M = 0xFFFF

	aR, aG, aB, aA := a.RGBA()
	bR, bG, bB, bA := b.RGBA()

	ar := float64(aR) / 0xFFFF
	ag := float64(aG) / 0xFFFF
	ab := float64(aB) / 0xFFFF
	aa := float64(aA) / 0xFFFF

	br := float64(bR) / 0xFFFF
	bg := float64(bG) / 0xFFFF
	bb := float64(bB) / 0xFFFF
	ba := float64(bA) / 0xFFFF

	oa := aa + ba*(1-aa)
	or := ar + br*(1-aa)
	og := ag + bg*(1-aa)
	ob := ab + bb*(1-aa)

	oR := uint16(0xFFFF * or * oa)
	oG := uint16(0xFFFF * og * oa)
	oB := uint16(0xFFFF * ob * oa)
	oA := uint16(0xFFFF * oa)

	return color.RGBA64{R: oR, G: oG, B: oB, A: oA}
}
Example #3
0
// Add add a color to average color and calculates a new average.
func (c *AvgColor) Add(cl color.Color) {
	c.count++
	r, g, b, _ := cl.RGBA()
	c.R = newAvg(c.R, r, c.count)
	c.G = newAvg(c.G, g, c.count)
	c.B = newAvg(c.B, b, c.count)
}
// https://code.google.com/p/gorilla/source/browse/color/hsv.go?r=ef489f63418265a7249b1d53bdc358b09a4a2ea0
func RGBToHSV(c color.Color) (h, s, v float64) {
	r, g, b, _ := c.RGBA()
	fR := float64(r) / 255
	fG := float64(g) / 255
	fB := float64(b) / 255
	max := math.Max(math.Max(fR, fG), fB)
	min := math.Min(math.Min(fR, fG), fB)
	d := max - min
	s, v = 0, max
	if max > 0 {
		s = d / max
	}
	if max == min {
		// Achromatic.
		h = 0
	} else {
		// Chromatic.
		switch max {
		case fR:
			h = (fG - fB) / d
			if fG < fB {
				h += 6
			}
		case fG:
			h = (fB-fR)/d + 2
		case fB:
			h = (fR-fG)/d + 4
		}
		h /= 6
	}
	return
}
Example #5
0
func (t *TreePalette) search(c color.Color, f func(leaf *TreePalette)) {
	r, g, b, _ := c.RGBA()
	var lt bool
	var s func(*TreePalette)
	s = func(t *TreePalette) {
		switch t.Type {
		case TLeaf:
			f(t)
			return
		case TSplitR:
			lt = r < t.Split
		case TSplitG:
			lt = g < t.Split
		case TSplitB:
			lt = b < t.Split
		}
		if lt {
			s(t.Low)
		} else {
			s(t.High)
		}
	}
	s(t)
	return
}
Example #6
0
File: color.go Project: oov/psd
func nCMYKA80Model(c color.Color) color.Color {
	if _, ok := c.(NCMYKA); ok {
		return c
	}
	r, g, b, a := c.RGBA()
	if a == 0 {
		return NCMYKA80{0xffff, 0xffff, 0xffff, 0xffff, 0}
	}
	w := r
	if w < g {
		w = g
	}
	if w < b {
		w = b
	}
	if w == 0 {
		return NCMYKA80{0xffff, 0xffff, 0xffff, 0xffff, uint16(a)}
	}
	cc := (w - r) * 0xffff / w
	mm := (w - g) * 0xffff / w
	yy := (w - b) * 0xffff / w
	kk := 0xffff - w
	if a == 0xffff {
		return NCMYKA80{uint16(0xffff - cc), uint16(0xffff - mm), uint16(0xffff - yy), uint16(0xffff - kk), 0xffff}
	}
	cc = (cc * 0xffff) / a
	mm = (mm * 0xffff) / a
	yy = (yy * 0xffff) / a
	kk = (kk * 0xffff) / a
	return NCMYKA80{uint16(0xffff - cc), uint16(0xffff - mm), uint16(0xffff - yy), uint16(0xffff - kk), uint16(a)}
}
Example #7
0
// calculates the Euclidean distance between two colors
func Distance(c1, c2 color.Color) float64 {
	r1, g1, b1, a1 := c1.RGBA()
	r2, g2, b2, a2 := c2.RGBA()
	sum := float64(uint64((r1-r2)*(r1-r2)) + uint64((g1-g2)*(g1-g2)) +
		uint64((b1-b2)*(b1-b2)) + uint64((a1-a2)*(a1-a2)))
	return math.Sqrt(sum)
}
func convert(c color.Color) color.RGBA {
	if rgba, ok := c.(color.RGBA); ok {
		return rgba
	}
	r, g, b, a := c.RGBA()
	return color.RGBA{uint8(r >> 8), uint8(g >> 8), uint8(b >> 8), uint8(a >> 8)}
}
Example #9
0
func rgbModel(c color.Color) color.Color {
	if _, ok := c.(RGB); ok {
		return c
	}
	r, g, b, _ := c.RGBA()
	return RGB{uint8(r >> 8), uint8(g >> 8), uint8(b >> 8)}
}
Example #10
0
// Returns an average of R,G,B from 0 to 1
func average(pixelColor color.Color) float64 {
	r, g, b, _ := pixelColor.RGBA()

	//fmt.Println("Sampling", r, g, b)

	return (float64(r)/65535.0 + float64(g)/65535.0 + float64(b)/65535.0) / 3.0
}
Example #11
0
func ScaleCol(col color.Color, scale float64) (result color.Color) {
	r, g, b, a := col.RGBA()
	r8 := uint8(math.Min(float64(r)*scale, 0xffff) / 256)
	g8 := uint8(math.Min(float64(g)*scale, 0xffff) / 256)
	b8 := uint8(math.Min(float64(b)*scale, 0xffff) / 256)
	return color.RGBA{r8, g8, b8, uint8(a >> 8)}
}
Example #12
0
File: rgb.go Project: kodabb/image2
func rgb48Model(c color.Color) color.Color {
	if _, ok := c.(RGB48); ok {
		return c
	}
	r, g, b, _ := c.RGBA()
	return RGB48{uint16(r), uint16(g), uint16(b)}
}
Example #13
0
func SetBackground(c color.Color) {
	if !headless {
		r, g, b, a := c.RGBA()

		Gl.ClearColor(float32(r), float32(g), float32(b), float32(a))
	}
}
Example #14
0
func (c *RGBA128) Add(rhs color.Color) {
	r, g, b, a := rhs.RGBA()
	c.R += r
	c.G += g
	c.B += b
	c.A += a
}
Example #15
0
func luma(pixel color.Color) float64 {
	r, g, b, _ := pixel.RGBA()

	return .2126*float64(r) +
		.7152*float64(g) +
		.0722*float64(b)
}
Example #16
0
func ColorC(c color.Color) {
	if c == nil {
		panic("nil color passed to ColorC")
	}
	r, g, b, a := c.RGBA()
	gl.Color4us(uint16(r), uint16(g), uint16(b), uint16(a))
}
Example #17
0
func Foreground24(c color.Color, s string) string {
	r, g, b, _ := c.RGBA()
	r = r >> 8
	g = g >> 8
	b = b >> 8
	return fmt.Sprintf("%c[38;2;%d;%d;%dm%s%c[0m", ESC, r, g, b, s, ESC)
}
Example #18
0
func colorcheck(t *testing.T, current, target color.Color) {
	r1, g1, b1, a1 := current.RGBA()
	r2, g2, b2, a2 := target.RGBA()
	if r1 != r2 || g1 != g2 || b1 != b2 || a1 != a2 {
		t.Errorf("Got [%d %d %d %d], expected [%d %d %d %d]", r1>>8, g1>>8, b1>>8, a1>>8, r2>>8, g2>>8, b2>>8, a2>>8)
	}
}
Example #19
0
func (hs *AutoSlicer) isEdge(pt color.Color) bool {
	var (
		r, g, b, a     = pt.RGBA()
		tr, tg, tb, ta = hs.thresh.RGBA()
	)
	return r <= tr && g <= tg && b <= tb && a >= ta
}
Example #20
0
// hexModel converts a Color to Hex.
func hexModel(c color.Color) color.Color {
	if _, ok := c.(Hex); ok {
		return c
	}
	r, g, b, _ := c.RGBA()
	return RGBToHex(uint8(r>>8), uint8(g>>8), uint8(b>>8))
}
Example #21
0
func (tree *oct) insert(c color.Color, size int, maxDepth uint8, strategy Strategy) {
	if len(tree.leaves()) <= size {
		r, g, b, _ := c.RGBA()
		tree.justInsert(&c, uint8(r), uint8(g), uint8(b), 0, maxDepth)

	} else {
		deepest := tree.deepest()
		toMerge := deepest[0]

		for _, node := range deepest {
			if strategy == LEAST {
				if node.count < toMerge.count {
					toMerge = node
				}
			} else {
				if node.count > toMerge.count {
					toMerge = node
				}
			}
		}

		toMerge.average()
		tree.insert(c, size, maxDepth, strategy)
	}
}
Example #22
0
// Convert Go colors to RGB values in range [0,1).
func normalize(col color.Color) (r, g, b float64) {
	ri, gi, bi, _ := col.RGBA()
	r = float64(ri) / float64(0x10000)
	g = float64(gi) / float64(0x10000)
	b = float64(bi) / float64(0x10000)
	return
}
func (f Framebuffer) convertToFb(c color.Color) (msb, lsb uint8) {
	r, g, b, _ := c.RGBA()
	out := uint16(r>>11)<<11 + uint16(g>>10)<<5 + uint16(b>>11)
	lsb = uint8(out >> 8)
	msb = uint8(out & 0xff)
	return
}
Example #24
0
func bgraModel(c color.Color) color.Color {
	if _, ok := c.(BGRA); ok {
		return c
	}
	r, g, b, a := c.RGBA()
	return BGRA{uint8(b >> 8), uint8(g >> 8), uint8(r >> 8), uint8(a >> 8)}
}
Example #25
0
// opacityString returns the opacity value of the given color.
func opacityString(clr color.Color) string {
	if clr == nil {
		clr = color.Black
	}
	_, _, _, a := clr.RGBA()
	return fmt.Sprintf("%.*g", pr, float64(a)/math.MaxUint16)
}
Example #26
0
func make_rgba(c color.Color) rgba {
	if c == nil {
		return 0
	}
	r, g, b, a := c.RGBA()
	return rgba(((a & 0xff) << 24) | ((r & 0xff) << 16) | ((g & 0xff) << 8) | (b & 0xff))
}
Example #27
0
func newSEColor(name string, value color.Color) *SimpleElement {
	r, g, b, a := value.RGBA()
	return &SimpleElement{
		StartElement: xml.StartElement{Name: xml.Name{Local: name}},
		value:        fmt.Sprintf("%02x%02x%02x%02x", a/256, b/256, g/256, r/256),
	}
}
Example #28
0
// ToGrayLightness converts color.Color c to grayscale using the lightness.
//
// The formula used for conversion is: Y = (max(r,g,b) + min(r,g,b)) / 2.
func ToGrayLightness(c color.Color) color.Gray {
	r, g, b, _ := c.RGBA()
	max := max(r, max(g, b))
	min := min(r, min(g, b))
	Y := (10*(min+max) + 5) / 20
	return color.Gray{uint8(Y >> 8)}
}
Example #29
0
/*
given a pixel, use its RGB value to determine grayscale value
return a black pixel with alpha based on inverse of the grayscale value
ie a pixel with a grayscale of 0 will set the alpha value to 255
*/
func setAlpha(pixel color.Color) color.RGBA {
	r, g, b, _ := pixel.RGBA()
	// swap 0 <-> 255, 1 <-> 254, etc.
	new_alpha := uint8(-1 * (int(((r+g+b)/3)>>8) - 255))
	// black pixel with alpha to set darkness
	return color.RGBA{0, 0, 0, new_alpha}
}
Example #30
-1
func (font *Font) updateTexture(texture uint32, text string, width int, height int, size float64, dpi float64, rgba color.Color) (int, int) {
	context := freetype.NewContext()
	context.SetFont(font.ttf)

	img := image.NewRGBA(image.Rect(0, 0, width, height))
	r, g, b, _ := rgba.RGBA()
	draw.Draw(img, img.Bounds(), image.NewUniform(color.RGBA{uint8(r), uint8(g), uint8(b), 0}), image.ZP, draw.Src)

	context.SetDst(img)
	context.SetClip(img.Bounds())
	context.SetSrc(image.NewUniform(rgba))
	context.SetFontSize(size)
	context.SetDPI(dpi)
	pixelBounds, _ := context.DrawString(text, freetype.Pt(0, height/2))

	gl.ActiveTexture(gl.TEXTURE0)
	gl.BindTexture(gl.TEXTURE_2D, texture)

	gl.TexSubImage2D(
		gl.TEXTURE_2D,
		0,
		0,
		0,
		int32(img.Rect.Size().X),
		int32(img.Rect.Size().Y),
		gl.RGBA,
		gl.UNSIGNED_BYTE,
		gl.Ptr(img.Pix))

	return int26_6Ceiling(pixelBounds.X + 0x3f), int26_6Ceiling(pixelBounds.Y + 0x3f)
}