Cairo Fill with Transparency

大兔子大兔子 提交于 2020-02-02 20:09:51

问题


I'm new to Cairo, trying to create text with transparent color and stroke.
stroke color's transparency works but text fill color transparency transparency_value doesn't work. If i reduce transparency_value , text color just gets darker(black) and increasing transparency_value makes text color brighter (green in my case)

 cairo_surface_t* surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 640, 480);
 cairo_t* cairo = cairo_create(surface);
 cairo_set_font_face(cairo, myfont_face);
 cairo_set_font_size(cairo, 25);
 cairo_text_extents_t extents;  
 cairo_text_extents(cairo, "Hello World", &extents);
 cairo_move_to(cairo, 200, 200);        
 cairo_text_path(cairo, "Hello World"); 
 double transparency_value = 0.5;
 cairo_set_source_rgba(cairo, 0,1,0,transparency_value ); //transparency doesn't work
 //cairo_fill(cairo);  //this didn't make a difference
 cairo_fill_preserve(cairo);
 cairo_set_source_rgba(cairo, 0.56, 0.76, 0.96, 0.5); //transparency works
 cairo_set_line_width(cairo, 1.5);
 cairo_stroke(cairo);

回答1:


Could it be that you are drawing your text outside of the surface? In the following example I added a call to cairo_move_to(cr, 200, 200) and now I get the following result. (This is written in Lua and uses https://github.com/pavouk/lgi to call into cairo; comments indicate things that I changed compared to your version)

local cairo = require("lgi").cairo
local surface = cairo.ImageSurface.create(cairo.Format.ARGB32, 640, 480)
local cr = cairo.Context(surface)
local myfont_face = cr:get_font_face() -- I have to get this from somewhere
cr:move_to(200, 200) -- I added this line to make something appear
cr:set_font_face(myfont_face)
cr:set_font_size(25)
cr:text_path("Hello World")
local transparency_value = 0.5
cr:set_source_rgba(0, 1, 0, transparency_value)
-- cr:fill()
cr:fill_preserve()
cr:set_source_rgba(0.65, 0.76, 0.96, 0.5)
cr:set_line_width(5) -- changed from 1.5 to 5 to make it more prominent
cr:stroke()
surface:write_to_png("/tmp/out.png")

Edit: And this is the result when I change transparency_value to 0.1. Clearly, the result is different and transparency works correctly (when zooming in, you still see some faint green in the middle).



来源:https://stackoverflow.com/questions/43299640/cairo-fill-with-transparency

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!