开罗充满透明度 [英] Cairo Fill with Transparency
问题描述
我是开罗的新手,试图用透明的颜色和笔触创建文字。
笔触颜色的透明度有效但文本填充颜色透明度 transparency_value
不起作用。
如果我减少 transparency_value
,文字颜色会变暗(黑色)并且增加 transparency_value
会使文字颜色更亮(在我的情况下为绿色)
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范围;
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); //透明度不起作用
// cairo_fill(cairo); //这没有什么区别
cairo_fill_preserve(cairo);
cairo_set_source_rgba(cairo,0.56,0.76,0.96,0.5); //透明度有效
cairo_set_line_width(cairo,1.5);
cairo_stroke(cairo);
可能是你在外面画你的文字表面?在下面的示例中,我添加了对 cairo_move_to(cr,200,200)
的调用,现在我得到以下结果。 (这是用Lua编写的,使用
编辑:这是我将 transparency_value
更改为0.1时的结果。显然,结果是不同的,透明度正常工作(当放大时,你仍会在中间看到一些微弱的绿色)。
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);
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).
这篇关于开罗充满透明度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!