2013-12-22 71 views
0

我有一個隨機函數生成的正弦波。將得到的點放入索引爲0至799(對應於我的x值)的數組中,並且所述索引的值與y的位置相對應。(C + GTK + Cairo)無法獲得線性漸變效果

現在,我試圖創建一個漸變,從綠色開始,100%alpha,並下降100px到綠色20%阿爾法。基本上會看起來像波浪向下淡出。我通過在我的線上每個點的y位置開始繪製1px寬x 100px的高梯度。

這是我的繪圖函數中的代碼。

void *do_draw(void *ptr) 
{ 
    /*** Prepare SIGALRM ***/ 
    siginfo_t info; 
    sigset_t sigset; 

    sigemptyset(&sigset); 
    sigaddset(&sigset, SIGALRM); 

    while(1) 
    { 
     while(sigwaitinfo(&sigset, &info) > 0) 
     { 
      currently_drawing = 1; 

      int width, height; 
      gdk_threads_enter(); 
      gdk_drawable_get_size(pixmap, &width, &height); 
      gdk_threads_leave(); 

      /*** Create surface to draw on ***/ 
      cairo_surface_t *cst = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, width, height); 
      cairo_t *cr = cairo_create(cst); 

      /*** Draw stuff ***/ 
      static int i = 0; 
      ++i; i = i % 800; 
      cairo_set_source_rgba (cr, .0, .0, .0, 1); 
      cairo_paint(cr); 

      int j = 0; 
      for (j = 0; j < 799; ++j) 
      { 
       double y = (double)(height + sine[j])/2; 

       cairo_pattern_t *pat1; 
       pat1 = cairo_pattern_create_linear(j, y, j, y + 100); 

       cairo_pattern_add_color_stop_rgba(pat1, 0.1, 0, 1, 0, 1); 
       cairo_pattern_add_color_stop_rgba(pat1, 0.9, 0, 1, 0, 0.2); 

       cairo_rectangle(cr, j, y, j, y + 100); 
       cairo_set_source(cr, pat1); 
       cairo_fill(cr); 

       cairo_pattern_destroy(pat1);  
      } 



      cairo_destroy(cr); 

      gdk_threads_enter(); 

      cairo_t *cr_pixmap = gdk_cairo_create(pixmap); 
      cairo_set_source_surface(cr_pixmap, cst, 0, 0); 
      cairo_paint(cr_pixmap); 
      cairo_destroy(cr_pixmap); 

      gdk_threads_leave(); 
      cairo_surface_destroy(cst); 

      currently_drawing = 0; 

     } 
    } 
} 

我現在得到的結果只是20%的alpha綠色100px粗線,但它確實遵循我的座標。我如何獲得這個漸變效果?我不瞭解梯度空間如何運作,我認爲。

回答

2

我把它變成了一些可以實際編譯和測試的獨立代碼,然後注意到你對cairo_rectangle()的調用是錯誤的。這個函數的參數是:

  • X
  • Ÿ
  • 寬度
  • 高度

您傳遞:

  • Ĵ
  • ÿ
  • Ĵ
  • Y + 100

所以較大的值導致較大的和大矩形被使用。我想,你希望這些參數來代替:

  • Ĵ
  • Ÿ

僅供參考,這裏是我的代碼:

#include <cairo.h> 
#include <math.h> 

int main(void) 
{ 
    int width = 800, height = 800; 
    int sine[800]; 

    int k; 
    for (k = 0; k < 800; k++) { 
     sine[k] = height * sin(k*M_PI/180); 
    } 

    /*** Create surface to draw on ***/ 
    cairo_surface_t *cst = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, width, height); 
    cairo_t *cr = cairo_create(cst); 

    /*** Draw stuff ***/ 
    static int i = 0; 
    ++i; i = i % 800; 
    cairo_set_source_rgba (cr, .0, .0, .0, 1); 
    cairo_paint(cr); 

    int j = 0; 
    for (j = 0; j < 799; ++j) 
    { 
     double y = (double)(height + sine[j])/2; 

     cairo_pattern_t *pat1; 
     pat1 = cairo_pattern_create_linear(j, y, j, y + 100); 

     cairo_pattern_add_color_stop_rgba(pat1, 0.1, 0, 1, 0, 1); 
     cairo_pattern_add_color_stop_rgba(pat1, 0.9, 0, 1, 0, 0.2); 

     cairo_rectangle(cr, j, y, 1, 100); 
     cairo_set_source(cr, pat1); 
     cairo_fill(cr); 

     cairo_pattern_destroy(pat1); 
    } 

    cairo_destroy(cr); 
    cairo_surface_write_to_png(cst, "t.png"); 
    cairo_surface_destroy(cst); 

    return 0; 
}