2 * ReactOS W32 Subsystem
3 * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003 ReactOS Team
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 /* $Id: color.c,v 1.26 2003/10/22 14:02:54 navaraf Exp $ */
21 // FIXME: Use PXLATEOBJ logicalToSystem instead of int *mapping
23 #undef WIN32_LEAN_AND_MEAN
25 #include <ddk/ntddk.h>
26 #include <ddk/winddi.h>
27 #include <win32k/brush.h>
28 #include <win32k/dc.h>
29 #include <win32k/color.h>
30 #include <win32k/pen.h>
31 #include "../eng/handle.h"
32 #include <include/inteng.h>
33 #include <include/color.h>
34 #include <include/palette.h>
35 #include <include/error.h>
38 #include <win32k/debug1.h>
40 int COLOR_gapStart
= 256;
41 int COLOR_gapEnd
= -1;
42 int COLOR_gapFilled
= 0;
45 static HPALETTE hPrimaryPalette
= 0; // used for WM_PALETTECHANGED
46 //static HPALETTE hLastRealizedPalette = 0; // UnrealizeObject() needs it
48 const PALETTEENTRY COLOR_sysPalTemplate
[NB_RESERVED_COLORS
] =
50 // first 10 entries in the system palette
51 // red green blue flags
52 { 0x00, 0x00, 0x00, PC_SYS_USED
},
53 { 0x80, 0x00, 0x00, PC_SYS_USED
},
54 { 0x00, 0x80, 0x00, PC_SYS_USED
},
55 { 0x80, 0x80, 0x00, PC_SYS_USED
},
56 { 0x00, 0x00, 0x80, PC_SYS_USED
},
57 { 0x80, 0x00, 0x80, PC_SYS_USED
},
58 { 0x00, 0x80, 0x80, PC_SYS_USED
},
59 { 0xc0, 0xc0, 0xc0, PC_SYS_USED
},
60 { 0xc0, 0xdc, 0xc0, PC_SYS_USED
},
61 { 0xa6, 0xca, 0xf0, PC_SYS_USED
},
63 // ... c_min/2 dynamic colorcells
64 // ... gap (for sparse palettes)
65 // ... c_min/2 dynamic colorcells
67 { 0xff, 0xfb, 0xf0, PC_SYS_USED
},
68 { 0xa0, 0xa0, 0xa4, PC_SYS_USED
},
69 { 0x80, 0x80, 0x80, PC_SYS_USED
},
70 { 0xff, 0x00, 0x00, PC_SYS_USED
},
71 { 0x00, 0xff, 0x00, PC_SYS_USED
},
72 { 0xff, 0xff, 0x00, PC_SYS_USED
},
73 { 0x00, 0x00, 0xff, PC_SYS_USED
},
74 { 0xff, 0x00, 0xff, PC_SYS_USED
},
75 { 0x00, 0xff, 0xff, PC_SYS_USED
},
76 { 0xff, 0xff, 0xff, PC_SYS_USED
} // last 10
79 const COLORREF SysColours
[] =
81 RGB(224, 224, 224) /* COLOR_SCROLLBAR */,
82 RGB(58, 110, 165) /* COLOR_BACKGROUND */,
83 RGB(0, 0, 128) /* COLOR_ACTIVECAPTION */,
84 RGB(128, 128, 128) /* COLOR_INACTIVECAPTION */,
85 RGB(192, 192, 192) /* COLOR_MENU */,
86 RGB(255, 255, 255) /* COLOR_WINDOW */,
87 RGB(0, 0, 0) /* COLOR_WINDOWFRAME */,
88 RGB(0, 0, 0) /* COLOR_MENUTEXT */,
89 RGB(0, 0, 0) /* COLOR_WINDOWTEXT */,
90 RGB(255, 255, 255) /* COLOR_CAPTIONTEXT */,
91 RGB(128, 128, 128) /* COLOR_ACTIVEBORDER */,
92 RGB(255, 255, 255) /* COLOR_INACTIVEBORDER */,
93 RGB(255, 255, 232) /* COLOR_APPWORKSPACE */,
94 RGB(224, 224, 224) /* COLOR_HILIGHT */,
95 RGB(0, 0, 128) /* COLOR_HILIGHTTEXT */,
96 RGB(192, 192, 192) /* COLOR_BTNFACE */,
97 RGB(128, 128, 128) /* COLOR_BTNSHADOW */,
98 RGB(192, 192, 192) /* COLOR_GRAYTEXT */,
99 RGB(0, 0, 0) /* COLOR_BTNTEXT */,
100 RGB(192, 192, 192) /* COLOR_INACTIVECAPTIONTEXT */,
101 RGB(255, 255, 255) /* COLOR_BTNHILIGHT */,
102 RGB(32, 32, 32) /* COLOR_3DDKSHADOW */,
103 RGB(192, 192, 192) /* COLOR_3DLIGHT */,
104 RGB(0, 0, 0) /* COLOR_INFOTEXT */,
105 RGB(255, 255, 192) /* COLOR_INFOBK */,
106 RGB(184, 180, 184) /* COLOR_ALTERNATEBTNFACE */,
107 RGB(0, 0, 255) /* COLOR_HOTLIGHT */,
108 RGB(16, 132, 208) /* COLOR_GRADIENTACTIVECAPTION */,
109 RGB(181, 181, 181) /* COLOR_GRADIENTINACTIVECAPTION */,
112 ULONG FASTCALL
NtGdiGetSysColor(int nIndex
)
115 const PALETTEENTRY
*p
= COLOR_sysPalTemplate
+ (nIndex
* sizeof(PALETTEENTRY
));
116 return RGB(p
->peRed
, p
->peGreen
, p
->peBlue
);
118 if (nIndex
< 0 || sizeof(SysColours
) / sizeof(SysColours
[0]) < nIndex
)
120 SetLastWin32Error(ERROR_INVALID_PARAMETER
);
124 return SysColours
[nIndex
];
128 HPEN STDCALL
NtGdiGetSysColorPen(int nIndex
)
132 memcpy(&Col
, COLOR_sysPalTemplate
+ nIndex
, sizeof(COLORREF
));
133 return(NtGdiCreatePen(PS_SOLID
, 1, Col
));
135 static HPEN SysPens
[sizeof(SysColours
) / sizeof(SysColours
[0])];
137 if (nIndex
< 0 || sizeof(SysColours
) / sizeof(SysColours
[0]) < nIndex
)
139 SetLastWin32Error(ERROR_INVALID_PARAMETER
);
143 /* FIXME should register this object with DeleteObject() so it
145 if (NULL
== SysPens
[nIndex
])
147 SysPens
[nIndex
] = (HPEN
)((DWORD
)NtGdiCreatePen(PS_SOLID
, 0, SysColours
[nIndex
]) | 0x00800000);
150 return SysPens
[nIndex
];
154 HBRUSH STDCALL
NtGdiGetSysColorBrush(int nIndex
)
158 memcpy(&Col
, COLOR_sysPalTemplate
+ nIndex
, sizeof(COLORREF
));
159 return(NtGdiCreateSolidBrush(Col
));
161 static HBRUSH SysBrushes
[sizeof(SysColours
) / sizeof(SysColours
[0])];
163 if (nIndex
< 0 || sizeof(SysColours
) / sizeof(SysColours
[0]) < nIndex
)
165 SetLastWin32Error(ERROR_INVALID_PARAMETER
);
169 /* FIXME should register this object with DeleteObject() so it
171 if (NULL
== SysBrushes
[nIndex
])
173 SysBrushes
[nIndex
] = (HBRUSH
) ((DWORD
)NtGdiCreateSolidBrush(SysColours
[nIndex
]) | 0x00800000);
176 return SysBrushes
[nIndex
];
182 const PALETTEENTRY
* FASTCALL
COLOR_GetSystemPaletteTemplate(void)
184 return (const PALETTEENTRY
*)&COLOR_sysPalTemplate
;
187 BOOL STDCALL
NtGdiAnimatePalette(HPALETTE hpal
,
190 CONST PPALETTEENTRY ppe
)
193 if( hPal != NtGdiGetStockObject(DEFAULT_PALETTE) )
195 PALETTEOBJ* palPtr = (PALETTEOBJ *)GDI_GetObjPtr(hPal, PALETTE_MAGIC);
196 if (!palPtr) return FALSE;
198 if( (StartIndex + NumEntries) <= palPtr->logpalette.palNumEntries )
201 for( u = 0; u < NumEntries; u++ )
202 palPtr->logpalette.palPalEntry[u + StartIndex] = PaletteColors[u];
203 PALETTE_Driver->pSetMapping(palPtr, StartIndex, NumEntries, hPal != hPrimaryPalette );
204 GDI_ReleaseObj(hPal);
207 GDI_ReleaseObj(hPal);
214 HPALETTE STDCALL
NtGdiCreateHalftonePalette(HDC hDC
)
219 WORD NumberOfEntries
;
220 PALETTEENTRY aEntries
[256];
223 Palette
.Version
= 0x300;
224 Palette
.NumberOfEntries
= 256;
225 NtGdiGetSystemPaletteEntries(hDC
, 0, 256, Palette
.aEntries
);
227 for (r
= 0; r
< 6; r
++) {
228 for (g
= 0; g
< 6; g
++) {
229 for (b
= 0; b
< 6; b
++) {
230 i
= r
+ g
*6 + b
*36 + 10;
231 Palette
.aEntries
[i
].peRed
= r
* 51;
232 Palette
.aEntries
[i
].peGreen
= g
* 51;
233 Palette
.aEntries
[i
].peBlue
= b
* 51;
238 for (i
= 216; i
< 246; i
++) {
239 int v
= (i
- 216) * 8;
240 Palette
.aEntries
[i
].peRed
= v
;
241 Palette
.aEntries
[i
].peGreen
= v
;
242 Palette
.aEntries
[i
].peBlue
= v
;
245 return NtGdiCreatePalette((LOGPALETTE
*)&Palette
);
248 HPALETTE STDCALL
NtGdiCreatePalette(CONST PLOGPALETTE palette
)
252 HPALETTE NewPalette
= PALETTE_AllocPalette(
254 palette
->palNumEntries
,
255 (PULONG
)palette
->palPalEntry
,
259 PalObj
= (PPALOBJ
) PALETTE_LockPalette(NewPalette
);
261 size
= sizeof(LOGPALETTE
) + (palette
->palNumEntries
* sizeof(PALETTEENTRY
));
262 PalObj
->logpalette
= ExAllocatePool(NonPagedPool
, size
);
263 memcpy(PalObj
->logpalette
, palette
, size
);
264 PALETTE_ValidateFlags(PalObj
->logpalette
->palPalEntry
, PalObj
->logpalette
->palNumEntries
);
265 PalObj
->logicalToSystem
= NULL
;
267 PALETTE_UnlockPalette(NewPalette
);
272 BOOL STDCALL
NtGdiGetColorAdjustment(HDC hDC
,
273 LPCOLORADJUSTMENT ca
)
278 COLORREF STDCALL
NtGdiGetNearestColor(HDC hDC
,
281 COLORREF nearest
= CLR_INVALID
;
288 HPALETTE hpal
= (dc
->w
.hPalette
) ? dc
->w
.hPalette
: NtGdiGetStockObject(DEFAULT_PALETTE
);
289 palObj
= (PPALOBJ
) PALETTE_LockPalette(hpal
);
296 nearest
= COLOR_LookupNearestColor(palObj
->logpalette
->palPalEntry
,
297 palObj
->logpalette
->palNumEntries
, Color
);
298 PALETTE_UnlockPalette(hpal
);
305 UINT STDCALL
NtGdiGetNearestPaletteIndex(HPALETTE hpal
,
308 PPALOBJ palObj
= (PPALOBJ
) PALETTE_LockPalette(hpal
);
313 /* Return closest match for the given RGB color */
314 index
= COLOR_PaletteLookupPixel(palObj
->logpalette
->palPalEntry
, palObj
->logpalette
->palNumEntries
, NULL
, Color
, FALSE
);
315 PALETTE_UnlockPalette(hpal
);
321 UINT STDCALL
NtGdiGetPaletteEntries(HPALETTE hpal
,
329 palPtr
= (PPALOBJ
) PALETTE_LockPalette(hpal
);
335 numEntries
= palPtr
->logpalette
->palNumEntries
;
336 if (numEntries
< StartIndex
+ Entries
)
338 Entries
= numEntries
- StartIndex
;
342 if (numEntries
<= StartIndex
)
344 PALETTE_UnlockPalette(hpal
);
347 memcpy(pe
, &palPtr
->logpalette
->palPalEntry
[StartIndex
], Entries
* sizeof(PALETTEENTRY
));
348 for (numEntries
= 0; numEntries
< Entries
; numEntries
++)
350 if (pe
[numEntries
].peFlags
& 0xF0)
352 pe
[numEntries
].peFlags
= 0;
357 PALETTE_UnlockPalette(hpal
);
361 UINT STDCALL
NtGdiGetSystemPaletteEntries(HDC hDC
,
369 if (!(dc = AccessUserObject(hdc))) return 0;
373 Entries = dc->GDIInfo->ulNumPalReg;
377 if (StartIndex >= dc->GDIInfo->ulNumPalReg)
383 if (StartIndex + Entries >= dc->GDIInfo->ulNumPalReg) Entries = dc->GDIInfo->ulNumPalReg - StartIndex;
385 for (i = 0; i < Entries; i++)
387 *(COLORREF*)(entries + i) = COLOR_GetSystemPaletteEntry(StartIndex + i);
391 // GDI_ReleaseObj(hdc);
393 // FIXME UNIMPLEMENTED;
397 UINT STDCALL
NtGdiGetSystemPaletteUse(HDC hDC
)
404 The RealizePalette function modifies the palette for the device associated with the specified device context. If the device context is a memory DC, the color table for the bitmap selected into the DC is modified. If the device context is a display DC, the physical palette for that device is modified.
406 A logical palette is a buffer between color-intensive applications and the system, allowing these applications to use as many colors as needed without interfering with colors displayed by other windows.
408 1= IF DRAWING TO A DEVICE
409 -- If it is a paletted bitmap, and is not an identity palette, then an XLATEOBJ is created between the logical palette and
411 -- If it is an RGB palette, then an XLATEOBJ is created between the RGB values and the system palette.
413 2= IF DRAWING TO A MEMORY DC\BITMAP
414 -- If it is a paletted bitmap, and is not an identity palette, then an XLATEOBJ is created between the logical palette and
416 -- If it is an RGB palette, then an XLATEOBJ is created between the RGB values and the dc palette.
418 UINT STDCALL
NtGdiRealizePalette(HDC hDC
)
420 PPALOBJ palPtr
, sysPtr
;
421 PPALGDI palGDI
, sysGDI
;
424 HPALETTE systemPalette
;
427 USHORT sysMode
, palMode
;
433 SurfGDI
= (PSURFGDI
)AccessInternalObject((ULONG
)dc
->Surface
);
434 systemPalette
= NtGdiGetStockObject((INT
)DEFAULT_PALETTE
);
435 palGDI
= PALETTE_LockPalette(dc
->w
.hPalette
);
436 palPtr
= (PPALOBJ
) palGDI
;
437 sysGDI
= PALETTE_LockPalette(systemPalette
);
438 sysPtr
= (PPALOBJ
) sysGDI
;
440 // Step 1: Create mapping of system palette\DC palette
441 realized
= PALETTE_SetMapping(palPtr
, 0, palPtr
->logpalette
->palNumEntries
,
442 (dc
->w
.hPalette
!= hPrimaryPalette
) ||
443 (dc
->w
.hPalette
== NtGdiGetStockObject(DEFAULT_PALETTE
)));
446 // The RealizePalette function modifies the palette for the device associated with the specified device context. If the
447 // device context is a memory DC, the color table for the bitmap selected into the DC is modified. If the device
448 // context is a display DC, the physical palette for that device is modified.
449 if(dc
->w
.flags
== DC_MEMORY
)
452 DbgPrint("win32k: realizepalette unimplemented step 2 for DC_MEMORY");
454 if(SurfGDI
->SetPalette
)
456 success
= SurfGDI
->SetPalette(dc
->PDev
, sysPtr
, 0, 0, sysPtr
->logpalette
->palNumEntries
);
460 // need to pass this to IntEngCreateXlate with palettes unlocked
461 sysMode
= sysGDI
->Mode
;
462 palMode
= palGDI
->Mode
;
463 PALETTE_UnlockPalette(systemPalette
);
464 PALETTE_UnlockPalette(dc
->w
.hPalette
);
466 // Step 3: Create the XLATEOBJ for device managed DCs
467 if(dc
->w
.flags
!= DC_MEMORY
)
470 palPtr
->logicalToSystem
= IntEngCreateXlate(sysGDI
->Mode
, palGDI
->Mode
, systemPalette
, dc
->w
.hPalette
);
478 BOOL STDCALL
NtGdiResizePalette(HPALETTE hpal
,
481 /* PPALOBJ palPtr = (PPALOBJ)AccessUserObject(hPal);
482 UINT cPrevEnt, prevVer;
483 INT prevsize, size = sizeof(LOGPALETTE) + (cEntries - 1) * sizeof(PALETTEENTRY);
484 PXLATEOBJ XlateObj = NULL;
486 if(!palPtr) return FALSE;
487 cPrevEnt = palPtr->logpalette->palNumEntries;
488 prevVer = palPtr->logpalette->palVersion;
489 prevsize = sizeof(LOGPALETTE) + (cPrevEnt - 1) * sizeof(PALETTEENTRY) + sizeof(int*) + sizeof(GDIOBJHDR);
490 size += sizeof(int*) + sizeof(GDIOBJHDR);
491 XlateObj = palPtr->logicalToSystem;
493 if (!(palPtr = GDI_ReallocObject(size, hPal, palPtr))) return FALSE;
497 PXLATEOBJ NewXlateObj = (int*) HeapReAlloc(GetProcessHeap(), 0, XlateObj, cEntries * sizeof(int));
498 if(NewXlateObj == NULL)
500 ERR("Can not resize logicalToSystem -- out of memory!");
501 GDI_ReleaseObj( hPal );
504 palPtr->logicalToSystem = NewXlateObj;
507 if(cEntries > cPrevEnt)
509 if(XlateObj) memset(palPtr->logicalToSystem + cPrevEnt, 0, (cEntries - cPrevEnt)*sizeof(int));
510 memset( (BYTE*)palPtr + prevsize, 0, size - prevsize );
511 PALETTE_ValidateFlags((PALETTEENTRY*)((BYTE*)palPtr + prevsize), cEntries - cPrevEnt );
513 palPtr->logpalette->palNumEntries = cEntries;
514 palPtr->logpalette->palVersion = prevVer;
515 // GDI_ReleaseObj( hPal );
522 * Select logical palette into device context.
523 * \param hDC handle to the device context
524 * \param hpal handle to the palette
525 * \param ForceBackground If this value is FALSE the logical palette will be copied to the device palette only when the applicatioon
526 * is in the foreground. If this value is TRUE then map the colors in the logical palette to the device
527 * palette colors in the best way.
528 * \return old palette
530 * \todo implement ForceBackground == TRUE
532 HPALETTE STDCALL
NtGdiSelectPalette(HDC hDC
,
534 BOOL ForceBackground
)
540 // FIXME: mark the palette as a [fore\back]ground pal
544 /* Check if this is a valid palette handle */
545 PalGDI
= PALETTE_LockPalette(hpal
);
548 PALETTE_UnlockPalette(hpal
);
549 oldPal
= dc
->w
.hPalette
;
550 dc
->w
.hPalette
= hpal
;
562 BOOL STDCALL
NtGdiSetColorAdjustment(HDC hDC
,
563 CONST LPCOLORADJUSTMENT ca
)
568 UINT STDCALL
NtGdiSetPaletteEntries(HPALETTE hpal
,
571 CONST LPPALETTEENTRY pe
)
576 palPtr
= (PPALOBJ
)PALETTE_LockPalette(hpal
);
577 if (!palPtr
) return 0;
579 numEntries
= palPtr
->logpalette
->palNumEntries
;
580 if (Start
>= numEntries
)
582 PALETTE_UnlockPalette(hpal
);
585 if (numEntries
< Start
+ Entries
)
587 Entries
= numEntries
- Start
;
589 memcpy(&palPtr
->logpalette
->palPalEntry
[Start
], pe
, Entries
* sizeof(PALETTEENTRY
));
590 PALETTE_ValidateFlags(palPtr
->logpalette
->palPalEntry
, palPtr
->logpalette
->palNumEntries
);
591 ExFreePool(palPtr
->logicalToSystem
);
592 palPtr
->logicalToSystem
= NULL
;
593 PALETTE_UnlockPalette(hpal
);
598 UINT STDCALL
NtGdiSetSystemPaletteUse(HDC hDC
,
604 BOOL STDCALL
NtGdiUnrealizeObject(HGDIOBJ hgdiobj
)
609 BOOL STDCALL
NtGdiUpdateColors(HDC hDC
)
615 if (!(dc = AccessUserObject(hDC))) return 0;
616 size = dc->GDIInfo->ulNumPalReg;
617 // GDI_ReleaseObj( hDC );
619 if (Callout.WindowFromDC)
621 hWnd = Callout.WindowFromDC( hDC );
623 // Docs say that we have to remap current drawable pixel by pixel
624 // but it would take forever given the speed of XGet/PutPixel.
625 if (hWnd && size) Callout.RedrawWindow( hWnd, NULL, 0, RDW_INVALIDATE );
627 // FIXME UNIMPLEMENTED
631 INT STDCALL
COLOR_PaletteLookupPixel(PALETTEENTRY
*palPalEntry
, INT size
,
632 PXLATEOBJ XlateObj
, COLORREF col
, BOOL skipReserved
)
634 int i
, best
= 0, diff
= 0x7fffffff;
637 for( i
= 0; i
< size
&& diff
; i
++ )
639 if(!(palPalEntry
[i
].peFlags
& PC_SYS_USED
) || (skipReserved
&& palPalEntry
[i
].peFlags
& PC_SYS_RESERVED
))
642 r
= palPalEntry
[i
].peRed
- GetRValue(col
);
643 g
= palPalEntry
[i
].peGreen
- GetGValue(col
);
644 b
= palPalEntry
[i
].peBlue
- GetBValue(col
);
648 if( r
< diff
) { best
= i
; diff
= r
; }
651 if (XlateObj
== NULL
)
654 return (XlateObj
->pulXlate
) ? (INT
)XlateObj
->pulXlate
[best
] : best
;
657 COLORREF STDCALL
COLOR_LookupNearestColor( PALETTEENTRY
* palPalEntry
, int size
, COLORREF color
)
659 unsigned char spec_type
= color
>> 24;
661 PALETTEENTRY
*COLOR_sysPal
= (PALETTEENTRY
*)ReturnSystemPalette();
663 // we need logical palette for PALETTERGB and PALETTEINDEX colorrefs
665 if( spec_type
== 2 ) /* PALETTERGB */
666 color
= *(COLORREF
*)(palPalEntry
+ COLOR_PaletteLookupPixel(palPalEntry
,size
,NULL
,color
,FALSE
));
668 else if( spec_type
== 1 ) /* PALETTEINDEX */
670 if( (i
= color
& 0x0000ffff) >= size
)
672 DbgPrint("RGB(%lx) : idx %d is out of bounds, assuming NULL\n", color
, i
);
673 color
= *(COLORREF
*)palPalEntry
;
675 else color
= *(COLORREF
*)(palPalEntry
+ i
);
679 return (0x00ffffff & *(COLORREF
*)(COLOR_sysPal
+ COLOR_PaletteLookupPixel(COLOR_sysPal
, 256, NULL
, color
, FALSE
)));
682 int STDCALL
COLOR_PaletteLookupExactIndex( PALETTEENTRY
* palPalEntry
, int size
,
686 BYTE r
= GetRValue(col
), g
= GetGValue(col
), b
= GetBValue(col
);
687 for( i
= 0; i
< size
; i
++ )
689 if( palPalEntry
[i
].peFlags
& PC_SYS_USED
) /* skips gap */
690 if(palPalEntry
[i
].peRed
== r
&& palPalEntry
[i
].peGreen
== g
&& palPalEntry
[i
].peBlue
== b
) return i
;