本文整理匯總了Golang中image.NewUniform函數的典型用法代碼示例。如果您正苦於以下問題:Golang NewUniform函數的具體用法?Golang NewUniform怎麽用?Golang NewUniform使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了NewUniform函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: GetText
func (ff *FontFace) GetText(text string) (t *Texture, err error) {
var (
src image.Image
bg image.Image
dst draw.Image
shortened draw.Image
pt fixed.Point26_6
w int
h int
)
src = image.NewUniform(ff.fg)
bg = image.NewUniform(ff.bg)
w = int(float32(len(text)) * ff.charw)
h = int(ff.charh)
dst = image.NewRGBA(image.Rect(0, 0, w, h))
draw.Draw(dst, dst.Bounds(), bg, image.ZP, draw.Src)
ff.context.SetSrc(src)
ff.context.SetDst(dst)
ff.context.SetClip(dst.Bounds())
pt = freetype.Pt(0, int(ff.charh))
if pt, err = ff.context.DrawString(text, pt); err != nil {
return
}
// if err = WritePNG("hello.png", dst); err != nil {
// return
// }
shortened = image.NewRGBA(image.Rect(0, 0, int(pt.X/64), h))
draw.Draw(shortened, shortened.Bounds(), dst, image.ZP, draw.Src)
t, err = GetTexture(shortened, gl.NEAREST)
return
}
示例2: GetImage
func (ff *FontFace) GetImage(text string) (img draw.Image, err error) {
var (
src image.Image
bg image.Image
dst draw.Image
pt fixed.Point26_6
w int
h int
)
src = image.NewUniform(ff.fg)
bg = image.NewUniform(ff.bg)
w = int(float32(len(text)) * ff.charw)
h = int(ff.charh)
dst = image.NewRGBA(image.Rect(0, 0, w, h))
draw.Draw(dst, dst.Bounds(), bg, image.ZP, draw.Src)
ff.context.SetSrc(src)
ff.context.SetDst(dst)
ff.context.SetClip(dst.Bounds())
pt = freetype.Pt(0, int(ff.charh+ff.offy))
if pt, err = ff.context.DrawString(text, pt); err != nil {
return
}
img = image.NewRGBA(image.Rect(0, 0, int(pt.X/64), int(pt.Y/64)))
draw.Draw(img, img.Bounds(), dst, image.Pt(0, -int(ff.offy)), draw.Src)
return
}
示例3: TestSrcMask
func TestSrcMask(t *testing.T) {
srcMask := image.NewRGBA(image.Rect(0, 0, 23, 1))
srcMask.SetRGBA(19, 0, color.RGBA{0x00, 0x00, 0x00, 0x7f})
srcMask.SetRGBA(20, 0, color.RGBA{0x00, 0x00, 0x00, 0xff})
srcMask.SetRGBA(21, 0, color.RGBA{0x00, 0x00, 0x00, 0x3f})
srcMask.SetRGBA(22, 0, color.RGBA{0x00, 0x00, 0x00, 0x00})
red := image.NewUniform(color.RGBA{0xff, 0x00, 0x00, 0xff})
blue := image.NewUniform(color.RGBA{0x00, 0x00, 0xff, 0xff})
dst := image.NewRGBA(image.Rect(0, 0, 6, 1))
Copy(dst, image.Point{}, blue, dst.Bounds(), Src, nil)
NearestNeighbor.Scale(dst, dst.Bounds(), red, image.Rect(0, 0, 3, 1), Over, &Options{
SrcMask: srcMask,
SrcMaskP: image.Point{20, 0},
})
got := [6]color.RGBA{
dst.RGBAAt(0, 0),
dst.RGBAAt(1, 0),
dst.RGBAAt(2, 0),
dst.RGBAAt(3, 0),
dst.RGBAAt(4, 0),
dst.RGBAAt(5, 0),
}
want := [6]color.RGBA{
{0xff, 0x00, 0x00, 0xff},
{0xff, 0x00, 0x00, 0xff},
{0x3f, 0x00, 0xc0, 0xff},
{0x3f, 0x00, 0xc0, 0xff},
{0x00, 0x00, 0xff, 0xff},
{0x00, 0x00, 0xff, 0xff},
}
if got != want {
t.Errorf("\ngot %v\nwant %v", got, want)
}
}
示例4: split
func split(img image.Image) (textImg, bgImg image.Image) {
size := img.Bounds().Max
text := image.NewGray(img.Bounds())
bg := image.NewRGBA(img.Bounds())
// copy image to background
draw.Src.Draw(bg, img.Bounds(), img, img.Bounds().Min)
// make text white
draw.Src.Draw(text, img.Bounds(), image.NewUniform(color.White), image.Point{})
for x := 0; x+32 <= size.X; x += 16 {
for y := 0; y+32 <= size.Y; y += 16 {
pt := image.Point{X: x, Y: y}
pt2 := image.Point{X: x + 32, Y: y + 32}
col, ok := isBitonal(img, pt)
if ok {
// fill uniform
draw.Src.Draw(bg, image.Rectangle{Min: pt, Max: pt2},
image.NewUniform(col), image.Point{})
// extract text
ref := asYCbCr(col)
for i := 0; i < 32; i++ {
for j := 0; j < 32; j++ {
col := asYCbCr(img.At(x+i, y+j))
val := float64(col.Y) / float64(ref.Y) * 256
if val >= 256 {
val = 255
}
text.SetGray(x+i, y+j, color.Gray{uint8(val)})
}
}
}
}
}
return text, bg
}
示例5: updateTexture
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)
}
示例6: Render
func (f *Font) Render(text string) *Texture {
width, height, yBearing := f.TextDimensions(text)
font := f.ttf
size := f.Size
// Colors
fg := image.NewUniform(color.NRGBA{f.FG.R, f.FG.G, f.FG.B, f.FG.A})
bg := image.NewUniform(color.NRGBA{f.BG.R, f.BG.G, f.BG.B, f.BG.A})
// Create the font context
c := freetype.NewContext()
nrgba := image.NewNRGBA(image.Rect(0, 0, width, height))
draw.Draw(nrgba, nrgba.Bounds(), bg, image.ZP, draw.Src)
c.SetDPI(dpi)
c.SetFont(font)
c.SetFontSize(size)
c.SetClip(nrgba.Bounds())
c.SetDst(nrgba)
c.SetSrc(fg)
// Draw the text.
pt := freetype.Pt(0, int(yBearing))
_, err := c.DrawString(text, pt)
if err != nil {
log.Println(err)
return nil
}
// Create texture
imObj := &ImageObject{nrgba}
return NewTexture(imObj)
}
示例7: textBox
// textBox renders t into a tight fitting image
func (ig *ImageGraphics) textBox(t string, font chart.Font) image.Image {
// Initialize the context.
fg := image.NewUniform(color.Alpha{0xff})
bg := image.NewUniform(color.Alpha{0x00})
width := ig.TextLen(t, font)
size := ig.relFontsizeToPixel(font.Size)
canvas := image.NewAlpha(image.Rect(0, 0, width, int(1.5*size+0.5)))
draw.Draw(canvas, canvas.Bounds(), bg, image.ZP, draw.Src)
c := freetype.NewContext()
c.SetDPI(dpi)
c.SetFont(ig.font)
c.SetFontSize(size)
c.SetClip(canvas.Bounds())
c.SetDst(canvas)
c.SetSrc(fg)
// Draw the text.
h := c.FUnitToPixelRU(ig.font.UnitsPerEm())
pt := freetype.Pt(0, h)
extent, err := c.DrawString(t, pt)
if err != nil {
log.Println(err)
return nil
}
// log.Printf("text %q, extent: %v", t, extent)
return canvas.SubImage(image.Rect(0, 0, int((extent.X+127)/256), h*5/4))
}
示例8: textBox
// textBox renders t into a tight fitting image
func (ig *ImageGraphics) textBox(t string, size int) image.Image {
// Initialize the context.
fg := image.NewUniform(color.Alpha{0xff})
bg := image.NewUniform(color.Alpha{0x00})
canvas := image.NewAlpha(image.Rect(0, 0, 400, 2*size))
draw.Draw(canvas, canvas.Bounds(), bg, image.ZP, draw.Src)
c := freetype.NewContext()
c.SetDPI(dpi)
c.SetFont(ig.font)
c.SetFontSize(float64(size))
c.SetClip(canvas.Bounds())
c.SetDst(canvas)
c.SetSrc(fg)
// Draw the text.
h := c.FUnitToPixelRU(ig.font.UnitsPerEm())
pt := freetype.Pt(0, h)
extent, err := c.DrawString(t, pt)
if err != nil {
log.Println(err)
return nil
}
// log.Printf("text %q, extent: %v", t, extent)
return canvas.SubImage(image.Rect(0, 0, int(extent.X/256), h*5/4))
}
示例9: Render
func (c *Context) Render(txt string, size float64, col color.Color) (*image.RGBA, error) {
bnd := c.fnt.Bounds(fixed.I(int(size + 0.5)))
lh := int26_6ToFloat64(bnd.Max.Y) - int26_6ToFloat64(bnd.Min.Y) - 0.5
c.ft.SetSrc(image.NewUniform(col))
c.ft.SetFontSize(size)
/* Render image to temporary buffer to determine final size */
tmp := nullImage{}
c.ft.SetDst(tmp)
c.ft.SetClip(tmp.Bounds())
p, err := c.ft.DrawString(txt, fixed.P(0, int(lh)))
if err != nil {
return nil, err
}
dst := image.NewRGBA(image.Rect(0, 0, int(int26_6ToFloat64(p.X)+0.5), int(lh)))
draw.Draw(dst, dst.Bounds(), image.NewUniform(color.RGBA{}), image.ZP, draw.Src)
c.ft.SetDst(dst)
c.ft.SetClip(dst.Bounds())
p, err = c.ft.DrawString(txt, fixed.P(0, int(size)))
if err != nil {
return nil, err
}
return dst, nil
}
示例10: TestDrawView
func TestDrawView(t *testing.T) {
const str = "Hello World!\nHow are you doing?"
f1 := truetype.NewFace(clearSans, &truetype.Options{DPI: 144})
f2 := truetype.NewFace(clearSansBoldItalic, &truetype.Options{DPI: 144})
red := image.NewUniform(color.RGBA{255, 0, 0, 255})
yellow := image.NewUniform(color.RGBA{255, 255, 0, 255})
view, _ := Render(
NewReader(
strings.NewReader("Hello World!\nHow are you doing?"),
Style{Offset: 0, Face: f1, Foreground: image.Black, Background: yellow},
Style{Offset: 10, Face: f2, Foreground: red, Background: yellow},
Style{Offset: 20, Face: f1, Foreground: image.Black, Background: image.White},
),
NewNaturalLayout(fixed.P(0, 0)),
)
size := view.Bounds.Max.Sub(view.Bounds.Min)
for _, a := range []Alignment{Left, Right, Center, Justify} {
dst := image.NewRGBA(image.Rect(0, 0, int(size.X>>6)+1, int(size.Y>>6)+1))
view.Align(a)
view.Draw(dst, LeftToRight)
saveTest(t, dst, "text.View.Draw_"+a.(fmt.Stringer).String()+".png")
}
}
示例11: Render
// Render renders the grid each time a value is sent on the render channel.
func Render(win wde.Window, g *grid.Grid, render chan bool) {
screen := win.Screen()
bounds := screen.Bounds()
grey := image.NewUniform(color.RGBA{R: 0xF9, G: 0xF9, B: 0xF9}) /// ### todo ### A: 0xFF?
for {
<-render
// Draw grid background.
draw.Draw(screen, bounds, grey, image.ZP, draw.Src)
draw.Draw(screen, bounds, imgGrid, image.ZP, draw.Over)
// Draw grid markers.
var img image.Image
for col := 0; col < g.Width(); col++ {
for row := 0; row < g.Height(); row++ {
switch g[col][row] {
case grid.MarkO:
img = imgO
case grid.MarkX:
img = imgX
default:
continue
}
pt := getCellPt(col, row, g[col][row])
rect := image.Rect(0, 0, img.Bounds().Dx(), img.Bounds().Dy())
draw.Draw(screen, rect.Add(pt), img, image.ZP, draw.Over)
}
}
// Flush screen updates.
win.FlushImage(bounds)
}
}
示例12: main
func main() {
const order = 8
const width = 1 << order
const margin = 10
bounds := image.Rect(-margin, -margin, width+2*margin, width+2*margin)
im := image.NewGray(bounds)
gBlack := color.Gray{0}
gWhite := color.Gray{255}
draw.Draw(im, bounds, image.NewUniform(gWhite), image.ZP, draw.Src)
for y := 0; y < width; y++ {
for x := 0; x < width; x++ {
if x&y == 0 {
im.SetGray(x, y, gBlack)
}
}
}
f, err := os.Create("sierpinski.png")
if err != nil {
fmt.Println(err)
return
}
if err = png.Encode(f, im); err != nil {
fmt.Println(err)
}
if err = f.Close(); err != nil {
fmt.Println(err)
}
}
示例13: main
func main() {
scale := width / (rMax - rMin)
height := int(scale * (iMax - iMin))
bounds := image.Rect(0, 0, width, height)
b := image.NewNRGBA(bounds)
draw.Draw(b, bounds, image.NewUniform(color.Black), image.ZP, draw.Src)
for x := 0; x < width; x++ {
for y := 0; y < height; y++ {
fEsc := mandelbrot(complex(
float64(x)/scale+rMin,
float64(y)/scale+iMin))
b.Set(x, y, color.NRGBA{uint8(red * fEsc),
uint8(green * fEsc), uint8(blue * fEsc), 255})
}
}
f, err := os.Create("mandelbrot.png")
if err != nil {
fmt.Println(err)
return
}
if err = png.Encode(f, b); err != nil {
fmt.Println(err)
}
if err = f.Close(); err != nil {
fmt.Println(err)
}
}
示例14: drawBarcode
func drawBarcode(img *image.Gray, unitSize, xPos, yPos float64, num int) {
drawSO(img, unitSize, xPos, yPos)
x := xPos + unitSize*4.0
// Draw reference bar
x += drawBar(img, unitSize, x, yPos, 0) + unitSize
// Print the form number below the barcode
ftContext.SetFontSize(7)
ftContext.SetSrc(image.NewUniform(color.Black))
ftContext.DrawString(fmt.Sprintf("%d", num), freetype.Pt(int(x), int(yPos+unitSize*7)))
// Draw bars
ds := toBase4(num, 9)
for i := len(ds) - 1; i >= 0; i-- {
x += drawBar(img, unitSize, x, yPos, ds[i]) + unitSize
}
// Draw checksum bars
ckBase10 := csumGen(num)
fmt.Println("ckBase10 = ", ckBase10)
ck := toBase4(ckBase10, 2)
fmt.Println("ck = ", ck)
for i := len(ck) - 1; i >= 0; i-- {
x += drawBar(img, unitSize, x, yPos, ck[i]) + unitSize
}
}
示例15: createSwatch
func createSwatch(col color.Color, size int) image.Image {
log.Printf("Creating swatch for %v\n", col)
bounds := image.Rect(0, 0, size, size)
img := image.NewNRGBA(bounds)
draw.Draw(img, img.Bounds(), image.NewUniform(col), img.Bounds().Min, draw.Src)
return img
}