C ++开罗+ RSVG - 绘图时设置SVG颜色

问题描述 投票:0回答:1

我想画一个单一颜色SVG到开罗的表面,但它是在白色的渲染,我想它是黑色的。开罗表面是一个8位alpha表面(CAIRO_FORMAT_A8)所以我只处理灰度。

我测试它的图像如下:https://commons.wikimedia.org/wiki/File:Octicons-flame.svg

这是我使用的渲染代码:

//Display buffer and Canvas
int stride;
Byte *displayBuffer;
cairo_surface_t *surface;

stride = cairo_format_stride_for_width (CAIRO_FORMAT_A8, gulPanelW);
displayBuffer = malloc (stride * gulPanelH);
surface = cairo_image_surface_create_for_data (displayBuffer, CAIRO_FORMAT_A8, gulPanelW, gulPanelH, stride);
cairo_t *cr = cairo_create (surface);

cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);

//Flame Surface and SVG Handle
RsvgHandle* flame_handle;
flame_handle = rsvg_handle_new_from_file("flame.svg", 0);

//Set canvas background to grey
cairo_set_source_rgba (cr, 1.0, 1.0, 1.0, 0.5);
cairo_paint (cr);

//Add initial data and output
cairo_select_font_face (cr, "serif", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_BOLD);
cairo_set_font_size (cr, 48.0);
cairo_set_source_rgba (cr, 1.0, 1.0, 1.0, 0.0);
cairo_move_to (cr, 700.0, 105.0);
cairo_rotate(cr, (M_PI/2.0));
cairo_show_text (cr, "Temperature");
cairo_rotate(cr, -(M_PI/2.0));

cairo_select_font_face (cr, "serif", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_BOLD);
cairo_set_font_size (cr, 200.0);
cairo_set_source_rgba (cr, 1.0, 1.0, 1.0, 0.0);
cairo_move_to (cr, 540.0, 34.0);
cairo_rotate(cr, (M_PI/2.0));
cairo_show_text (cr, "19.4");
cairo_rotate(cr, -(M_PI/2.0));

cairo_translate (cr, 476.0, 200.0);
cairo_scale (cr, 0.07, 0.07);
cairo_rotate(cr, (M_PI/2.0));
rsvg_handle_render_cairo (flame_handle, cr);
cairo_rotate (cr, -(M_PI/2.0));
cairo_scale (cr, 1.0, 1.0);

cairo_surface_write_to_png (surface, "output.png");

此输出如下:

Cairo Output

但正如你所看到的,火焰在白色的呈现,我想这是一个黑色的 - 或特定的alpha值。

任何帮助,将不胜感激,在开罗我找不到SVG的颜色多的信息。

非常感谢。

svg colors cairo librsvg
1个回答
0
投票

cairo_maskcairo_mask_surface可以使用另一个表面的alpha通道作为掩模用于绘图操作。下面的示例使用这种“画事”与特定的α水平。

我还添加了必要的东西,使这个独立的例子,摆脱了不必要的cairo_image_surface_create_for_data的。

#include <cairo.h>
#include <librsvg/rsvg.h>
#include <math.h>

#define gulPanelW 800
#define gulPanelH 480

int main()
{
    //Display buffer and Canvas
    cairo_surface_t *surface;

    surface = cairo_image_surface_create (CAIRO_FORMAT_A8, gulPanelW, gulPanelH);
    cairo_t *cr = cairo_create (surface);

    cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);

    //Flame Surface and SVG Handle
    RsvgHandle* flame_handle;
    flame_handle = rsvg_handle_new_from_file("flame.svg", 0);

    //Set canvas background to grey
    cairo_set_source_rgba (cr, 1.0, 1.0, 1.0, 0.5);
    cairo_paint (cr);

    //Add initial data and output
    cairo_select_font_face (cr, "serif", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_BOLD);
    cairo_set_font_size (cr, 48.0);
    cairo_set_source_rgba (cr, 1.0, 1.0, 1.0, 0.0);
    cairo_move_to (cr, 700.0, 105.0);
    cairo_rotate(cr, (M_PI/2.0));
    cairo_show_text (cr, "Temperature");
    cairo_rotate(cr, -(M_PI/2.0));

    cairo_select_font_face (cr, "serif", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_BOLD);
    cairo_set_font_size (cr, 200.0);
    cairo_set_source_rgba (cr, 1.0, 1.0, 1.0, 0.0);
    cairo_move_to (cr, 540.0, 34.0);
    cairo_rotate(cr, (M_PI/2.0));
    cairo_show_text (cr, "19.4");
    cairo_rotate(cr, -(M_PI/2.0));

    // Draw the flame icon to a temporary surface
    cairo_save (cr);
    cairo_translate (cr, 476.0, 200.0);
    cairo_scale (cr, 0.07, 0.07);
    cairo_rotate(cr, (M_PI/2.0));
    cairo_push_group (cr);
    rsvg_handle_render_cairo (flame_handle, cr);
    cairo_pattern_t *p = cairo_pop_group (cr);

    // Use the temporary surface as a mask for drawing this color
    cairo_set_source_rgba (cr, 1.0, 1.0, 1.0, 0.0);
    cairo_mask(cr, p);
    cairo_restore (cr);

    // Clean up
    cairo_surface_write_to_png (surface, "output.png");
    cairo_pattern_destroy (p);
    cairo_destroy (cr);
    cairo_surface_destroy (surface);
    return 0;
}
热门问题
推荐问题
最新问题