9fans.net/go@v0.0.5/draw/allocimagemix.go (about)

     1  package draw
     2  
     3  // AllocImageMix is used to allocate background colors.
     4  // It returns a 1×1 replicated image whose pixel is the result of
     5  // mixing the two colors in a one to three ratio.
     6  // On 8-bit color-mapped displays, it returns a 2×2 replicated image
     7  // with one pixel colored the color one and the other three
     8  // with three.  (This simulates a wider range of tones than can
     9  // be represented by a single pixel value on a color-mapped
    10  // display.)
    11  func (d *Display) AllocImageMix(color1, color3 Color) *Image {
    12  	d.mu.Lock()
    13  	defer d.mu.Unlock()
    14  	if d.ScreenImage.Depth <= 8 { // create a 2x2 texture
    15  		t, _ := d.allocImage(Rect(0, 0, 1, 1), d.ScreenImage.Pix, false, color1)
    16  		b, _ := d.allocImage(Rect(0, 0, 2, 2), d.ScreenImage.Pix, true, color3)
    17  		b.draw(Rect(0, 0, 1, 1), t, nil, ZP)
    18  		t.free()
    19  		return b
    20  	}
    21  
    22  	// use a solid color, blended using alpha
    23  	if d.qmask == nil {
    24  		d.qmask, _ = d.allocImage(Rect(0, 0, 1, 1), GREY8, true, 0x3F3F3FFF)
    25  	}
    26  	t, _ := d.allocImage(Rect(0, 0, 1, 1), d.ScreenImage.Pix, true, color1)
    27  	b, _ := d.allocImage(Rect(0, 0, 1, 1), d.ScreenImage.Pix, true, color3)
    28  	b.draw(b.R, t, d.qmask, ZP)
    29  	t.free()
    30  	return b
    31  }