4 * Copyright 1993,1994 Alexandre Julliard
6 static char Copyright[] = "Copyright Alexandre Julliard, 1993,1994";
14 #define MAXINT INT_MAX
21 /* #define DEBUG_PALETTE */
24 static WORD SystemPaletteUse
= SYSPAL_STATIC
; /* currently not considered */
26 /***********************************************************************
27 * CreatePalette (GDI.360)
29 HPALETTE
CreatePalette( LOGPALETTE
* palette
)
31 PALETTEOBJ
* palettePtr
;
35 size
= sizeof(LOGPALETTE
) + (palette
->palNumEntries
- 1) * sizeof(PALETTEENTRY
);
36 hpalette
= GDI_AllocObject( sizeof(GDIOBJHDR
) + size
, PALETTE_MAGIC
);
37 if (!hpalette
) return 0;
38 palettePtr
= (PALETTEOBJ
*) GDI_HEAP_LIN_ADDR( hpalette
);
39 memcpy( &palettePtr
->logpalette
, palette
, size
);
44 /***********************************************************************
45 * GetPaletteEntries (GDI.363)
47 WORD
GetPaletteEntries( HPALETTE hpalette
, WORD start
, WORD count
,
48 LPPALETTEENTRY entries
)
53 palPtr
= (PALETTEOBJ
*) GDI_GetObjPtr( hpalette
, PALETTE_MAGIC
);
54 if (!palPtr
) return 0;
55 numEntries
= palPtr
->logpalette
.palNumEntries
;
56 if (start
>= numEntries
) return 0;
57 if (start
+count
> numEntries
) count
= numEntries
- start
;
58 memcpy( entries
, &palPtr
->logpalette
.palPalEntry
[start
],
59 count
* sizeof(PALETTEENTRY
) );
64 /***********************************************************************
65 * SetPaletteEntries (GDI.364)
67 WORD
SetPaletteEntries( HPALETTE hpalette
, WORD start
, WORD count
,
68 LPPALETTEENTRY entries
)
73 palPtr
= (PALETTEOBJ
*) GDI_GetObjPtr( hpalette
, PALETTE_MAGIC
);
74 if (!palPtr
) return 0;
75 numEntries
= palPtr
->logpalette
.palNumEntries
;
76 if (start
>= numEntries
) return 0;
77 if (start
+count
> numEntries
) count
= numEntries
- start
;
78 memcpy( &palPtr
->logpalette
.palPalEntry
[start
], entries
,
79 count
* sizeof(PALETTEENTRY
) );
84 /***********************************************************************
85 * SetSystemPaletteUse (GDI.373)
86 * Should this be per DC rather than system wide?
87 * Currently, it does not matter as the use is only set and returned,
88 * but not taken into account
90 WORD
SetSystemPaletteUse( HDC hdc
, WORD use
)
92 WORD old
=SystemPaletteUse
;
93 printf("SetSystemPaletteUse(%04X,%04X) // empty stub !!!\n", hdc
, use
);
98 /***********************************************************************
99 * GetSystemPaletteUse (GDI.374)
101 WORD
GetSystemPaletteUse( HDC hdc
)
103 printf("GetSystemPaletteUse(%04X) // empty stub !!!\n", hdc
);
104 return SystemPaletteUse
;
108 /***********************************************************************
109 * GetSystemPaletteEntries (GDI.375)
111 WORD
GetSystemPaletteEntries( HDC hdc
, WORD start
, WORD count
,
112 LPPALETTEENTRY entries
)
118 if (!(dc
= (DC
*) GDI_GetObjPtr( hdc
, DC_MAGIC
))) return 0;
119 if (start
>= dc
->w
.devCaps
->sizePalette
) return 0;
120 if (start
+count
>= dc
->w
.devCaps
->sizePalette
)
121 count
= dc
->w
.devCaps
->sizePalette
- start
;
122 for (i
= 0; i
< count
; i
++)
124 color
.pixel
= start
+ i
;
125 XQueryColor( display
, COLOR_WinColormap
, &color
);
126 entries
[i
].peRed
= color
.red
>> 8;
127 entries
[i
].peGreen
= color
.green
>> 8;
128 entries
[i
].peBlue
= color
.blue
>> 8;
129 entries
[i
].peFlags
= 0;
135 /***********************************************************************
136 * GetNearestPaletteIndex (GDI.370)
138 WORD
GetNearestPaletteIndex( HPALETTE hpalette
, COLORREF color
)
140 int i
, minDist
, dist
;
143 PALETTEENTRY
* entry
;
146 palPtr
= (PALETTEOBJ
*) GDI_GetObjPtr( hpalette
, PALETTE_MAGIC
);
147 if (!palPtr
) return 0;
149 if ((COLOR_WinColormap
!= DefaultColormapOfScreen(screen
)) &&
150 (hpalette
== STOCK_DEFAULT_PALETTE
))
152 if ((color
& 0xffffff) == 0) return 0; /* Entry 0 is black */
153 if ((color
& 0xffffff) == 0xffffff) /* Max entry is white */
154 return palPtr
->logpalette
.palNumEntries
- 1;
157 r
= GetRValue(color
);
158 g
= GetGValue(color
);
159 b
= GetBValue(color
);
161 entry
= palPtr
->logpalette
.palPalEntry
;
162 for (i
= 0, minDist
= MAXINT
; minDist
!=0 &&
163 i
< palPtr
->logpalette
.palNumEntries
; i
++)
165 if (entry
->peFlags
!= 0xff)
167 dist
= (r
- entry
->peRed
) * (r
- entry
->peRed
) +
168 (g
- entry
->peGreen
) * (g
- entry
->peGreen
) +
169 (b
- entry
->peBlue
) * (b
- entry
->peBlue
);
178 dprintf_palette(stddeb
,"GetNearestPaletteIndex(%x,%06lx): returning %d\n",
179 hpalette
, color
, index
);
184 /***********************************************************************
187 int PALETTE_GetObject( PALETTEOBJ
* palette
, int count
, LPSTR buffer
)
189 if (count
> sizeof(WORD
)) count
= sizeof(WORD
);
190 memcpy( buffer
, &palette
->logpalette
.palNumEntries
, count
);
195 /***********************************************************************
196 * GDISelectPalette (GDI.361)
198 HPALETTE
GDISelectPalette( HDC hdc
, HPALETTE hpal
)
203 dprintf_palette(stddeb
, "GDISelectPalette: %d %d\n", hdc
, hpal
);
204 if (!(dc
= (DC
*) GDI_GetObjPtr( hdc
, DC_MAGIC
))) return 0;
205 prev
= dc
->w
.hPalette
;
206 dc
->w
.hPalette
= hpal
;
207 if (hpal
!= STOCK_DEFAULT_PALETTE
) COLOR_SetMapping( dc
, 0, 0, 0 );
208 else RealizeDefaultPalette( hdc
); /* Always realize default palette */
213 /***********************************************************************
214 * GDIRealizePalette (GDI.362)
216 UINT
GDIRealizePalette( HDC hdc
)
218 dprintf_palette(stdnimp
, "GDIRealizePalette: %d\n", hdc
);
223 /***********************************************************************
224 * SelectPalette (USER.282)
226 HPALETTE
SelectPalette(HDC hDC
, HPALETTE hPal
, BOOL bForceBackground
)
228 return GDISelectPalette( hDC
, hPal
);
232 /***********************************************************************
233 * RealizePalette (USER.283)
235 UINT
RealizePalette(HDC hDC
)
237 return GDIRealizePalette( hDC
);