4 * Copyright 1993,1994 Alexandre Julliard
7 static char Copyright
[] = "Copyright Alexandre Julliard, 1993,1994";
20 #define MAXINT INT_MAX
26 extern void COLOR_SetMapping( DC
*dc
, HANDLE map
, WORD size
); /* color.c */
28 extern Colormap COLOR_WinColormap
;
31 /***********************************************************************
32 * CreatePalette (GDI.360)
34 HPALETTE
CreatePalette( LOGPALETTE
* palette
)
36 PALETTEOBJ
* palettePtr
;
40 size
= sizeof(LOGPALETTE
) + (palette
->palNumEntries
- 1) * sizeof(PALETTEENTRY
);
41 hpalette
= GDI_AllocObject( sizeof(GDIOBJHDR
) + size
, PALETTE_MAGIC
);
42 if (!hpalette
) return 0;
43 palettePtr
= (PALETTEOBJ
*) GDI_HEAP_ADDR( hpalette
);
44 memcpy( &palettePtr
->logpalette
, palette
, size
);
49 /***********************************************************************
50 * GetPaletteEntries (GDI.363)
52 WORD
GetPaletteEntries( HPALETTE hpalette
, WORD start
, WORD count
,
53 LPPALETTEENTRY entries
)
58 palPtr
= (PALETTEOBJ
*) GDI_GetObjPtr( hpalette
, PALETTE_MAGIC
);
59 if (!palPtr
) return 0;
60 numEntries
= palPtr
->logpalette
.palNumEntries
;
61 if (start
>= numEntries
) return 0;
62 if (start
+count
> numEntries
) count
= numEntries
- start
;
63 memcpy( entries
, &palPtr
->logpalette
.palPalEntry
[start
],
64 count
* sizeof(PALETTEENTRY
) );
69 /***********************************************************************
70 * SetPaletteEntries (GDI.364)
72 WORD
SetPaletteEntries( HPALETTE hpalette
, WORD start
, WORD count
,
73 LPPALETTEENTRY entries
)
78 palPtr
= (PALETTEOBJ
*) GDI_GetObjPtr( hpalette
, PALETTE_MAGIC
);
79 if (!palPtr
) return 0;
80 numEntries
= palPtr
->logpalette
.palNumEntries
;
81 if (start
>= numEntries
) return 0;
82 if (start
+count
> numEntries
) count
= numEntries
- start
;
83 memcpy( &palPtr
->logpalette
.palPalEntry
[start
], entries
,
84 count
* sizeof(PALETTEENTRY
) );
89 /***********************************************************************
90 * GetSystemPaletteEntries (GDI.375)
92 WORD
GetSystemPaletteEntries( HDC hdc
, WORD start
, WORD count
,
93 LPPALETTEENTRY entries
)
99 if (!(dc
= (DC
*) GDI_GetObjPtr( hdc
, DC_MAGIC
))) return 0;
100 if (start
>= dc
->w
.devCaps
->sizePalette
) return 0;
101 if (start
+count
>= dc
->w
.devCaps
->sizePalette
)
102 count
= dc
->w
.devCaps
->sizePalette
- start
;
103 for (i
= 0; i
< count
; i
++)
105 color
.pixel
= start
+ i
;
106 XQueryColor( display
, COLOR_WinColormap
, &color
);
107 entries
[i
].peRed
= color
.red
>> 8;
108 entries
[i
].peGreen
= color
.green
>> 8;
109 entries
[i
].peBlue
= color
.blue
>> 8;
110 entries
[i
].peFlags
= 0;
116 /***********************************************************************
117 * GetNearestPaletteIndex (GDI.370)
119 WORD
GetNearestPaletteIndex( HPALETTE hpalette
, COLORREF color
)
121 int i
, minDist
, dist
;
124 PALETTEENTRY
* entry
;
127 palPtr
= (PALETTEOBJ
*) GDI_GetObjPtr( hpalette
, PALETTE_MAGIC
);
128 if (!palPtr
) return 0;
130 if ((COLOR_WinColormap
!= DefaultColormapOfScreen(screen
)) &&
131 (hpalette
== STOCK_DEFAULT_PALETTE
))
133 if ((color
& 0xffffff) == 0) return 0; /* Entry 0 is black */
134 if ((color
& 0xffffff) == 0xffffff) /* Max entry is white */
135 return palPtr
->logpalette
.palNumEntries
- 1;
138 r
= GetRValue(color
);
139 g
= GetGValue(color
);
140 b
= GetBValue(color
);
142 entry
= palPtr
->logpalette
.palPalEntry
;
143 for (i
= 0, minDist
= MAXINT
; minDist
!=0 &&
144 i
< palPtr
->logpalette
.palNumEntries
; i
++)
146 if (entry
->peFlags
!= 0xff)
148 dist
= (r
- entry
->peRed
) * (r
- entry
->peRed
) +
149 (g
- entry
->peGreen
) * (g
- entry
->peGreen
) +
150 (b
- entry
->peBlue
) * (b
- entry
->peBlue
);
160 printf( "GetNearestPaletteIndex(%x,%06x) : returning %d\n",
161 hpalette
, color
, index
);
167 /***********************************************************************
170 int PALETTE_GetObject( PALETTEOBJ
* palette
, int count
, LPSTR buffer
)
172 if (count
> sizeof(WORD
)) count
= sizeof(WORD
);
173 memcpy( buffer
, &palette
->logpalette
.palNumEntries
, count
);
178 /***********************************************************************
179 * GDISelectPalette (GDI.361)
181 HPALETTE
GDISelectPalette( HDC hdc
, HPALETTE hpal
)
187 printf( "GDISelectPalette: %d %d\n", hdc
, hpal
);
189 if (!(dc
= (DC
*) GDI_GetObjPtr( hdc
, DC_MAGIC
))) return 0;
190 prev
= dc
->w
.hPalette
;
191 dc
->w
.hPalette
= hpal
;
192 if (hpal
!= STOCK_DEFAULT_PALETTE
) COLOR_SetMapping( dc
, 0, 0 );
193 else RealizeDefaultPalette( hdc
); /* Always realize default palette */
198 /***********************************************************************
199 * GDIRealizePalette (GDI.362)
201 UINT
GDIRealizePalette( HDC hdc
)
204 printf( "GDIRealizePalette: %d\n", hdc
);
210 /***********************************************************************
211 * SelectPalette (USER.282)
213 HPALETTE
SelectPalette(HDC hDC
, HPALETTE hPal
, BOOL bForceBackground
)
215 return GDISelectPalette( hDC
, hPal
);
219 /***********************************************************************
220 * RealizePalette (USER.283)
222 UINT
RealizePalette(HDC hDC
)
224 return GDIRealizePalette( hDC
);