1
  
2
  
3
  
4
  
5
  
6
  
7
  
8
  
9
  
10
  
11
  
12
  
13
  
14
  
15
  
16
  
17
  
18
  
19
  
20
  
21
  
22
  
23
  
24
  
25
  
26
  
27
  
28
  
29
  
30
  
31
  
32
  
33
  
34
  
35
  
36
  
37
  
38
  
39
  
40
  
41
  
42
  
43
  
44
  
45
  
46
  
47
  
48
  
49
  
50
  
51
  
52
  
53
  
54
  
55
  
56
  
57
  
58
  
59
  
60
  
61
  
62
  
63
  
64
  
65
  
66
  
67
  
68
  
69
  
70
  
71
  
72
  
73
  
74
  
75
  
76
  
77
  
78
  
79
  
80
  
81
  
82
  
83
  
84
  
85
  
86
  
87
  
88
  
89
  
90
  
91
  
92
  
93
  
94
  
95
  
96
  
97
  
98
  
99
  
100
  
101
  
102
  
103
  
104
  
105
  
106
  
107
  
108
  
109
  
110
  
111
  
112
  
113
  
114
  
115
  
116
  
117
  
118
  
/* -*- C -*- */ 
class GDK2.Color; 
 
//! The GDK2.Color object is used to represent a color. 
//! When you call GDK2.Color(r,g,b) the color will be allocated 
//! from the X-server. The constructor can return an  exception if there are 
//! no more colors to allocate. 
//! NOIMG 
 
void create(object|int color_or_r,int|void g,int|void b) 
//! r g and b are in the range 0 to 255, inclusive. 
//! If color is specified, it should be an Image.Color object, and the 
//! only argument. 
{ 
  INT_TYPE r,g,b; 
  GdkColormap *col=gdk_colormap_get_system(); 
  GdkColor *c; 
  pgtk2_verify_setup(); 
  pgtk2_verify_not_inited(); 
  if (args==1) { 
    struct object *o; 
    get_all_args(NULL,args,"%o",&o); 
    if (!get_color_from_pikecolor(o,&r,&g,&b)) 
      Pike_error("Bad argument 1 to GDK2.Color(). Exptected color object\n"); 
  } else { 
    get_all_args(NULL,args,"%i%i%i",&r,&g,&b); 
    r*=257; 
    g*=257; 
    b*=257; 
  } 
 
  c=g_malloc(sizeof(GdkColor)); 
  if (c==NULL) 
    SIMPLE_OUT_OF_MEMORY_ERROR("",sizeof(GdkColor)); 
  THIS->owned = 1; 
  THIS->obj=(void *)c; 
  c->red=(guint16)r; 
  c->green=(guint16)g; 
  c->blue=(guint16)b; 
  c->pixel=0; 
/*  if(!gdk_color_alloc( col, c )) */ 
  if (!gdk_colormap_alloc_color(col,c,TRUE,FALSE)) { 
    g_free(c); 
    THIS->obj=0; 
    Pike_error("Failed to allocate color.\n"); 
  } 
  pgtk2_pop_n_elems(args); 
} 
 
array(int) rgb() 
//! Returns the red green and blue color components as an array. 
{ 
  pgtk2_pop_n_elems(args); 
  push_int(((GdkColor *)THIS->obj)->red/257); 
  push_int(((GdkColor *)THIS->obj)->green/257); 
  push_int(((GdkColor *)THIS->obj)->blue/257); 
  f_aggregate(3); 
} 
 
int red() 
//! Returns the red color component. 
{ 
  pgtk2_pop_n_elems(args); 
  push_int(((GdkColor *)THIS->obj)->red/257); 
} 
 
int green() 
//! Returns the green color component. 
{ 
  pgtk2_pop_n_elems(args); 
  push_int(((GdkColor *)THIS->obj)->green/257); 
} 
 
int blue() 
//! Returns the blue color component. 
{ 
  pgtk2_pop_n_elems(args); 
  push_int(((GdkColor *)THIS->obj)->blue/257); 
} 
 
int pixel() 
//! Returns the pixel value of the color. See <a href="gdk.image.xml#set_pixel">GDK2.Image->set_pixel</a>. 
{ 
  pgtk2_pop_n_elems(args); 
  push_int(((GdkColor *)THIS->obj)->pixel); 
} 
 
Image.Color.Color image_color_object() 
//! Return a Image.Color.Color instance.<br /> 
//! This gives better precision than the rgb function. 
{ 
  char colorname[4+4+4+4]; 
  sprintf((char *)colorname,"#%04x%04x%04x", 
          ((GdkColor *)THIS->obj)->red, 
          ((GdkColor *)THIS->obj)->green, 
          ((GdkColor *)THIS->obj)->blue); 
 
  pgtk2_pop_n_elems(args); 
  pgtk2_get_image_module(); 
  pgtk2_index_stack("Color"); 
  pgtk2_index_stack((char *)colorname); 
} 
 
void _destruct() 
//! Destroys the color object. Please note that this function does 
//! not free the color from the X-colormap (in case of pseudocolor) 
//! right now. 
{ 
  if (THIS->obj && THIS->owned) { 
    g_free(THIS->obj); 
    THIS->obj=0; 
    THIS->owned=0; 
  } 
  pgtk2_pop_n_elems(args); 
  push_int(0); 
}