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 */
27 /***********************************************************************
28 * PALETTE_GetNearestIndexAndColor
30 static WORD
PALETTE_GetNearestIndexAndColor(HPALETTE hpalette
, COLORREF
*color
)
38 palPtr
= (PALETTEOBJ
*) GDI_GetObjPtr( hpalette
, PALETTE_MAGIC
);
39 if (!palPtr
) return 0;
41 if ((COLOR_WinColormap
!= DefaultColormapOfScreen(screen
)) &&
42 (hpalette
== STOCK_DEFAULT_PALETTE
))
44 if ((*color
& 0xffffff) == 0) return 0; /* Entry 0 is black */
45 if ((*color
& 0xffffff) == 0xffffff) /* Max entry is white */
46 return palPtr
->logpalette
.palNumEntries
- 1;
49 r
= GetRValue(*color
);
50 g
= GetGValue(*color
);
51 b
= GetBValue(*color
);
53 entry
= palPtr
->logpalette
.palPalEntry
;
54 for (i
= 0, minDist
= MAXINT
; minDist
!=0 &&
55 i
< palPtr
->logpalette
.palNumEntries
; i
++)
57 if (entry
->peFlags
!= 0xff)
59 dist
= (r
- entry
->peRed
) * (r
- entry
->peRed
) +
60 (g
- entry
->peGreen
) * (g
- entry
->peGreen
) +
61 (b
- entry
->peBlue
) * (b
- entry
->peBlue
);
70 entry
= &palPtr
->logpalette
.palPalEntry
[index
];
71 *color
= RGB( entry
->peRed
, entry
->peGreen
, entry
->peBlue
);
76 /***********************************************************************
77 * CreatePalette (GDI.360)
79 HPALETTE
CreatePalette( LOGPALETTE
* palette
)
81 PALETTEOBJ
* palettePtr
;
85 size
= sizeof(LOGPALETTE
) + (palette
->palNumEntries
- 1) * sizeof(PALETTEENTRY
);
86 hpalette
= GDI_AllocObject( sizeof(GDIOBJHDR
) + size
, PALETTE_MAGIC
);
87 if (!hpalette
) return 0;
88 palettePtr
= (PALETTEOBJ
*) GDI_HEAP_LIN_ADDR( hpalette
);
89 memcpy( &palettePtr
->logpalette
, palette
, size
);
94 /***********************************************************************
95 * GetPaletteEntries (GDI.363)
97 WORD
GetPaletteEntries( HPALETTE hpalette
, WORD start
, WORD count
,
98 LPPALETTEENTRY entries
)
103 palPtr
= (PALETTEOBJ
*) GDI_GetObjPtr( hpalette
, PALETTE_MAGIC
);
104 if (!palPtr
) return 0;
105 numEntries
= palPtr
->logpalette
.palNumEntries
;
106 if (start
>= numEntries
) return 0;
107 if (start
+count
> numEntries
) count
= numEntries
- start
;
108 memcpy( entries
, &palPtr
->logpalette
.palPalEntry
[start
],
109 count
* sizeof(PALETTEENTRY
) );
114 /***********************************************************************
115 * SetPaletteEntries (GDI.364)
117 WORD
SetPaletteEntries( HPALETTE hpalette
, WORD start
, WORD count
,
118 LPPALETTEENTRY entries
)
123 palPtr
= (PALETTEOBJ
*) GDI_GetObjPtr( hpalette
, PALETTE_MAGIC
);
124 if (!palPtr
) return 0;
125 numEntries
= palPtr
->logpalette
.palNumEntries
;
126 if (start
>= numEntries
) return 0;
127 if (start
+count
> numEntries
) count
= numEntries
- start
;
128 memcpy( &palPtr
->logpalette
.palPalEntry
[start
], entries
,
129 count
* sizeof(PALETTEENTRY
) );
133 /***********************************************************************
134 * ResizePalette (GDI.368)
136 BOOL
ResizePalette(HPALETTE hPal
, UINT cEntries
)
138 fprintf(stdnimp
,"ResizePalette: empty stub! \n");
142 /***********************************************************************
143 * AnimatePalette (GDI.367)
145 void AnimatePalette(HPALETTE hPal
, UINT StartIndex
, UINT NumEntries
,
146 LPPALETTEENTRY PaletteColors
)
148 fprintf(stdnimp
,"AnimatePalette: empty stub! \n");
151 /***********************************************************************
152 * SetSystemPaletteUse (GDI.373)
153 * Should this be per DC rather than system wide?
154 * Currently, it does not matter as the use is only set and returned,
155 * but not taken into account
157 WORD
SetSystemPaletteUse( HDC hdc
, WORD use
)
159 WORD old
=SystemPaletteUse
;
160 printf("SetSystemPaletteUse("NPFMT
",%04X) // empty stub !!!\n", hdc
, use
);
161 SystemPaletteUse
=use
;
165 /***********************************************************************
166 * GetSystemPaletteUse (GDI.374)
168 WORD
GetSystemPaletteUse( HDC hdc
)
170 printf("GetSystemPaletteUse("NPFMT
") // empty stub !!!\n", hdc
);
171 return SystemPaletteUse
;
175 /***********************************************************************
176 * GetSystemPaletteEntries (GDI.375)
178 WORD
GetSystemPaletteEntries( HDC hdc
, WORD start
, WORD count
,
179 LPPALETTEENTRY entries
)
185 if (!(dc
= (DC
*) GDI_GetObjPtr( hdc
, DC_MAGIC
))) return 0;
186 if (start
>= dc
->w
.devCaps
->sizePalette
) return 0;
187 if (start
+count
>= dc
->w
.devCaps
->sizePalette
)
188 count
= dc
->w
.devCaps
->sizePalette
- start
;
189 for (i
= 0; i
< count
; i
++)
191 color
.pixel
= start
+ i
;
192 XQueryColor( display
, COLOR_WinColormap
, &color
);
193 entries
[i
].peRed
= color
.red
>> 8;
194 entries
[i
].peGreen
= color
.green
>> 8;
195 entries
[i
].peBlue
= color
.blue
>> 8;
196 entries
[i
].peFlags
= 0;
202 /***********************************************************************
203 * GetNearestPaletteIndex (GDI.370)
205 WORD
GetNearestPaletteIndex( HPALETTE hpalette
, COLORREF color
)
207 WORD index
= PALETTE_GetNearestIndexAndColor( hpalette
, &color
);
208 dprintf_palette(stddeb
,"GetNearestPaletteIndex("NPFMT
",%06lx): returning %d\n",
209 hpalette
, color
, index
);
214 /***********************************************************************
215 * GetNearestColor (GDI.154)
217 COLORREF
GetNearestColor( HDC hdc
, COLORREF color
)
219 COLORREF nearest
= color
;
222 if (!(dc
= (DC
*) GDI_GetObjPtr( hdc
, DC_MAGIC
))) return 0;
223 PALETTE_GetNearestIndexAndColor( dc
->w
.hPalette
, &nearest
);
224 dprintf_palette(stddeb
,"GetNearestColor(%06lx): returning %06lx\n",
230 /***********************************************************************
233 int PALETTE_GetObject( PALETTEOBJ
* palette
, int count
, LPSTR buffer
)
235 if (count
> sizeof(WORD
)) count
= sizeof(WORD
);
236 memcpy( buffer
, &palette
->logpalette
.palNumEntries
, count
);
241 /***********************************************************************
242 * GDISelectPalette (GDI.361)
244 HPALETTE
GDISelectPalette( HDC hdc
, HPALETTE hpal
)
249 dprintf_palette(stddeb
, "GDISelectPalette: "NPFMT
" "NPFMT
"\n", hdc
, hpal
);
250 if (!(dc
= (DC
*) GDI_GetObjPtr( hdc
, DC_MAGIC
))) return 0;
251 prev
= dc
->w
.hPalette
;
252 dc
->w
.hPalette
= hpal
;
253 if (hpal
!= STOCK_DEFAULT_PALETTE
) COLOR_SetMapping( dc
, 0, 0, 0 );
254 else RealizeDefaultPalette( hdc
); /* Always realize default palette */
259 /***********************************************************************
260 * GDIRealizePalette (GDI.362)
262 UINT
GDIRealizePalette( HDC hdc
)
264 dprintf_palette(stdnimp
, "GDIRealizePalette: "NPFMT
"\n", hdc
);
269 /***********************************************************************
270 * SelectPalette (USER.282)
272 HPALETTE
SelectPalette(HDC hDC
, HPALETTE hPal
, BOOL bForceBackground
)
274 return GDISelectPalette( hDC
, hPal
);
278 /***********************************************************************
279 * RealizePalette (USER.283)
281 UINT
RealizePalette(HDC hDC
)
283 return GDIRealizePalette( hDC
);