summarylogtreecommitdiffstats
path: root/24-bit-color.patch
blob: 422d48cd11a177464da65ad56e629a5a1d276312 (plain)
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
--- config.h.in
+++ config.h.in
@@ -291,6 +291,9 @@
 /* Define if you want 256-color support */
 #undef USE_256_COLORS
 
+/* Define if you want 24-bit color support */
+#undef USE_24_BIT_COLOR
+
 /* Enable extensions on AIX 3, Interix.  */
 #ifndef _ALL_SOURCE
 # undef _ALL_SOURCE
--- configure
+++ configure
@@ -734,6 +734,7 @@
 enable_assert
 enable_warnings
 enable_256_color
+enable_24_bit_color
 enable_unicode3
 enable_combining
 enable_xft
@@ -1408,6 +1409,7 @@
   --enable-assert         enable assertions
   --enable-warnings       turn on g++ warnings
   --enable-256-color      enable 256-color support
+  --enable-24-bit-color   enable 24-bit color support
   --enable-unicode3       use 21 instead of 16 bits to represent unicode characters
   --enable-combining      enable composition of base and combining characters
   --enable-xft            enable xft support on systems that have it
@@ -5129,6 +5131,21 @@
 
 fi
 
+support_24_bit_color=no
+# Check whether --enable-24-bit-color was given.
+if test ${enable_24_bit_color+y}
+then :
+  enableval=$enable_24_bit_color; if test x$enableval = xyes; then
+    support_24_bit_color=yes
+  fi
+fi
+
+if test x$support_24_bit_color = xyes; then
+
+printf "%s\n" "#define USE_24_BIT_COLOR 1" >>confdefs.h
+
+fi
+
 # Check whether --enable-unicode3 was given.
 if test ${enable_unicode3+y}
 then :
--- configure.ac
+++ configure.ac
@@ -197,6 +197,16 @@
   AC_DEFINE(USE_256_COLORS, 1, Define if you want 256-color support)
 fi
 
+support_24_bit_color=no
+AC_ARG_ENABLE(24-bit-color,
+  [  --enable-24-bit-color   enable 24-bit color support],
+  [if test x$enableval = xyes; then
+    support_24_bit_color=yes
+  fi])
+if test x$support_24_bit_color = xyes; then
+  AC_DEFINE(USE_24_BIT_COLOR, 1, Define if you want 24-bit color support)
+fi
+
 AC_ARG_ENABLE(unicode3,
   [  --enable-unicode3       use 21 instead of 16 bits to represent unicode characters],
   [if test x$enableval = xyes -o x$enableval = xno; then
--- README.configure
+++ README.configure
@@ -9,8 +9,8 @@
 
     --enable-everything
         Add (or remove) support for all non-multichoice options listed in
-        "./configure --help", except for "--enable-assert" and
-        "--enable-256-color".
+        "./configure --help", except for "--enable-assert",
+        "--enable-256-color" and "--enable-24-bit-color".
 
         You can specify this and then disable options you do not like by
         *following* this with the appropriate "--disable-..." arguments, or
@@ -239,6 +239,20 @@
 
         It also results in higher memory usage and can slow down urxvt
         dramatically when more than six fonts are in use by a terminal
+        instance.
+
+    --enable-24-bit-color (default: off)
+        Enable use of 24-bit colors through
+            SGR 38 ; 2 ; R ; G ; B  m
+            SGR 48 ; 2 ; R ; G ; B  m
+
+        This switch should break termcap/terminfo compatibility to
+        "TERM=rxvt-unicode-256color", and consequently set "TERM" to
+        "rxvt-unicode-24bit" by default but there is no termcap/terminfo
+        for 24-bit color support
+
+        It also results in higher memory usage and can slow down urxvt
+        dramatically when more than six fonts are in use by a terminal
         instance.
 
     --with-name=NAME (default: urxvt)
--- src/command.C
+++ src/command.C
@@ -1824,11 +1824,11 @@
   if (rs[Rs_fade])
     {
       if (!first_time)
-        pix_colors_focused [idx].free (this);
+        lookup_color(idx, pix_colors_focused).free (this);
 
       rgba c;
-      pix_colors [Color_fade].get (c);
-      pix_colors_focused [idx].fade (this, atoi (rs[Rs_fade]), pix_colors_unfocused [idx], c);
+      lookup_color(Color_fade, pix_colors).get (c);
+      lookup_color(idx, pix_colors_focused).fade (this, atoi (rs[Rs_fade]), lookup_color(idx, pix_colors_unfocused), c);
     }
 #endif
 }
@@ -3424,7 +3424,7 @@
   if (str[0] == '?' && !str[1])
     {
       rgba c;
-      pix_colors_focused[color].get (c);
+      lookup_color(color, pix_colors_focused).get (c);
       char rgba_str[32];
 
 #if XFT
@@ -3933,7 +3933,7 @@
 {
   unsigned int i;
   short rendset;
-  int rendstyle;
+  rend_t rendstyle;
 
   if (nargs == 0)
     {
@@ -4039,6 +4039,20 @@
             break;
 
           case 38: // set fg color, ISO 8613-6
+#if USE_24_BIT_COLOR
+            if (nargs > i + 2 && arg[i + 1] == 5)
+              {
+                scr_color ((unsigned int) (minCOLOR + arg[i + 2]), Color_fg);
+                i += 2;
+              }
+            else if (nargs > i + 4 && arg[i + 1] == 2)
+              {
+                unsigned int r = arg[i + 2], g = arg[i + 3], b = arg[i + 4];
+                scr_color_rgb (r, g, b, Color_fg);
+                i += 4;
+              }
+#endif
+            break;
           case 48: // set bg color, ISO 8613-6
             {
               unsigned int fgbg = arg[i] == 38 ? Color_fg : Color_bg;
@@ -4051,6 +4065,14 @@
 
                   scr_color (idx, fgbg);
                 }
+#if USE_24_BIT_COLOR
+              else if (nargs > i + 4 && arg[i + 1] == 2)
+                {
+                  unsigned int r = arg[i + 2], g = arg[i + 3], b = arg[i + 4];
+                  scr_color_rgb (r, g, b, Color_bg);
+                  i += 4;
+                }
+#endif
               else if (nargs > i + 4 && arg[i + 1] == 2)
                 {
                   unsigned int r = arg[i + 2];
--- src/init.C
+++ src/init.C
@@ -1138,7 +1138,7 @@
 
   for (i = 0; i < NRS_COLORS; i++)
     if (const char *name = rs[Rs_color + i])
-      set_color (pix_colors [i], name);
+      set_color (lookup_color(i, pix_colors), name);
 
   /*
    * get scrollBar shadow colors
@@ -1147,13 +1147,13 @@
    * from the fvwm window manager.
    */
 #ifdef RXVT_SCROLLBAR
-  pix_colors [Color_scroll].fade (this, 50, pix_colors [Color_bottomShadow]);
+  lookup_color(Color_scroll, pix_colors).fade (this, 50, lookup_color(Color_bottomShadow, pix_colors));
 
   rgba cscroll;
-  pix_colors [Color_scroll].get (cscroll);
+  lookup_color(Color_scroll, pix_colors).get (cscroll);
 
   /* topShadowColor */
-  if (!pix_colors[Color_topShadow].set (this,
+  if (!lookup_color(Color_topShadow, pix_colors).set (this,
                    rgba (
                      min ((int)rgba::MAX_CC, max (cscroll.r / 5, cscroll.r) * 7 / 5),
                      min ((int)rgba::MAX_CC, max (cscroll.g / 5, cscroll.g) * 7 / 5),
@@ -1395,8 +1395,8 @@
   window_calc (0, 0);
 
   /* sub-window placement & size in rxvt_term::resize_all_windows () */
-  attributes.background_pixel = pix_colors_focused [Color_border];
-  attributes.border_pixel     = pix_colors_focused [Color_border];
+  attributes.background_pixel = lookup_color(Color_border, pix_colors_focused);
+  attributes.border_pixel     = lookup_color(Color_border, pix_colors_focused);
   attributes.colormap         = cmap;
 
   top = XCreateWindow (dpy, parent,
@@ -1493,8 +1493,8 @@
                             window_vt_x, window_vt_y,
                             vt_width, vt_height,
                             0,
-                            pix_colors_focused[Color_fg],
-                            pix_colors_focused[Color_bg]);
+                            lookup_color(Color_fg, pix_colors_focused),
+                            lookup_color(Color_bg, pix_colors_focused));
 
   attributes.bit_gravity = NorthWestGravity;
   XChangeWindowAttributes (dpy, vt, CWBitGravity, &attributes);
@@ -1511,8 +1511,8 @@
   vt_ev.start (display, vt);
 
   /* graphics context for the vt window */
-  gcvalue.foreground         = pix_colors[Color_fg];
-  gcvalue.background         = pix_colors[Color_bg];
+  gcvalue.foreground         = lookup_color(Color_fg, pix_colors);
+  gcvalue.background         = lookup_color(Color_bg, pix_colors);
   gcvalue.graphics_exposures = 0;
 
   gc = XCreateGC (dpy, vt,
--- src/main.C
+++ src/main.C
@@ -251,10 +251,10 @@
       for (int i = 0; i < TOTAL_COLORS; i++)
         if (ISSET_PIXCOLOR (i))
           {
-            pix_colors_focused   [i].free (this);
+            lookup_color(i, pix_colors_focused).free (this);
 #if OFF_FOCUS_FADING
             if (rs[Rs_fade])
-              pix_colors_unfocused [i].free (this);
+              lookup_color(i, pix_colors_unfocused).free (this);
 #endif
           }
 
@@ -970,8 +970,8 @@
         }
     }
 
-  pix_colors_focused[idx].free (this);
-  set_color (pix_colors_focused[idx], color);
+  lookup_color(idx, pix_colors_focused).free (this);
+  set_color (lookup_color(idx, pix_colors_focused), color);
 
 done:
   /*TODO: handle Color_BD, scrollbar background, etc. */
@@ -988,12 +988,12 @@
   XColor fg, bg;
 
   (ISSET_PIXCOLOR (Color_pointer_fg)
-     ? pix_colors_focused[Color_pointer_fg]
-     : pix_colors_focused[Color_fg]).get (fg);
+     ? lookup_color(Color_pointer_fg, pix_colors_focused)
+     : lookup_color(Color_fg, pix_colors_focused)).get (fg);
 
   (ISSET_PIXCOLOR (Color_pointer_bg)
-     ? pix_colors_focused[Color_pointer_bg]
-     : pix_colors_focused[Color_bg]).get (bg);
+     ? lookup_color(Color_pointer_bg, pix_colors_focused)
+     : lookup_color(Color_bg, pix_colors_focused)).get (bg);
 
   XRecolorCursor (dpy, TermWin_cursor, &fg, &bg);
 }
@@ -1012,14 +1012,14 @@
   char *env_colorfgbg;
 
   for (i = Color_Black; i <= Color_White; i++)
-    if (pix_colors[Color_fg] == pix_colors[i])
+    if (lookup_color(Color_fg, pix_colors) == lookup_color(i, pix_colors))
       {
         sprintf (fstr, "%d", i - Color_Black);
         break;
       }
 
   for (i = Color_Black; i <= Color_White; i++)
-    if (pix_colors[Color_bg] == pix_colors[i])
+    if (lookup_color(Color_bg, pix_colors) == lookup_color(i, pix_colors))
       {
         sprintf (bstr, "%d", i - Color_Black);
 #if HAVE_IMG
@@ -1048,8 +1048,8 @@
 void
 rxvt_term::alias_color (int dst, int src)
 {
-  pix_colors[dst].free (this);
-  pix_colors[dst].set (this, rs[Rs_color + dst] = rs[Rs_color + src]);
+  lookup_color(dst, pix_colors).free (this);
+  lookup_color(dst, pix_colors).set (this, rs[Rs_color + dst] = rs[Rs_color + src]);
 }
 
 #ifdef SMART_RESIZE
@@ -1203,8 +1203,8 @@
 void
 rxvt_term::im_set_color (unsigned long &fg, unsigned long &bg)
 {
-  fg = pix_colors [Color_fg];
-  bg = pix_colors [Color_bg];
+  fg = lookup_color(Color_fg, pix_colors);
+  bg = lookup_color(Color_bg, pix_colors);
 }
 
 void
--- src/rxvtfont.C
+++ src/rxvtfont.C
@@ -243,20 +243,20 @@
 
 # ifdef HAVE_IMG
       if (term->bg_img
-          && !term->pix_colors[color].is_opaque ()
+          && !term->lookup_color(color, term->pix_colors).is_opaque ()
           && ((dst = XftDrawPicture (d))))
         {
           XClearArea (disp, d, x, y, w, h, false);
 
-          Picture solid_color_pict = XftDrawSrcPicture (d, &term->pix_colors[color].c);
+          Picture solid_color_pict = XftDrawSrcPicture (d, &term->lookup_color(color, term->pix_colors).c);
           XRenderComposite (disp, PictOpOver, solid_color_pict, None, dst, 0, 0, 0, 0, x, y, w, h);
         }
       else
 # endif
-        XftDrawRect (d, &term->pix_colors[color].c, x, y, w, h);
+        XftDrawRect (d, &term->lookup_color(color, term->pix_colors).c, x, y, w, h);
 
 #else
-      XSetForeground (disp, gc, term->pix_colors[color]);
+      XSetForeground (disp, gc, term->lookup_color(color, term->pix_colors));
       XFillRectangle (disp, d, gc, x, y, w, h);
 #endif
     }
@@ -343,7 +343,7 @@
 
   clear_rect (d, x, y, term->fwidth * len, term->fheight, bg);
 
-  XSetForeground (disp, gc, term->pix_colors[fg]);
+  XSetForeground (disp, gc, term->lookup_color(fg, term->pix_colors));
 
   while (len)
     {
@@ -1065,7 +1065,7 @@
   int base = ascent; // sorry, incorrect: term->fbase;
 
   XGCValues v;
-  v.foreground = term->pix_colors[fg];
+  v.foreground = term->lookup_color(fg, term->pix_colors);
   v.font = f->fid;
 
   if (enc2b)
@@ -1074,7 +1074,7 @@
 
       if (bg == Color_bg && !slow)
         {
-          v.background = term->pix_colors[bg];
+          v.background = term->lookup_color(bg, term->pix_colors);
           XChangeGC (disp, gc, GCForeground | GCBackground | GCFont, &v);
           XDrawImageString16 (disp, d, gc, x, y + base, xc, len);
         }
@@ -1106,7 +1106,7 @@
 
       if (bg == Color_bg && !slow)
         {
-          v.background = term->pix_colors[bg];
+          v.background = term->lookup_color(bg, term->pix_colors);
           XChangeGC (disp, gc, GCForeground | GCBackground | GCFont, &v);
           XDrawImageString (disp, d, gc, x, y + base, xc, len);
         }
@@ -1528,7 +1528,7 @@
 
           if (term->bg_img
               && (bg == Color_transparent || bg == Color_bg
-                  || (bg >= 0 && !term->pix_colors[bg].is_opaque () && ((dst = XftDrawPicture (d2))))))
+                  || (bg >= 0 && !term->lookup_color(bg, term->pix_colors).is_opaque () && ((dst = XftDrawPicture (d2))))))
             {
               int src_x = x, src_y = y;
 
@@ -1565,7 +1565,7 @@
 
               if (dst)
                 {
-                  Picture solid_color_pict = XftDrawSrcPicture (d2, &term->pix_colors[bg].c);
+                  Picture solid_color_pict = XftDrawSrcPicture (d2, &term->lookup_color(bg, term->pix_colors).c);
 
                   // dst can only be set when bg >= 0
                   XRenderComposite (disp, PictOpOver, solid_color_pict, None, dst, 0, 0, 0, 0, 0, 0, w, h);
@@ -1573,9 +1573,9 @@
             }
           else
 #endif
-            XftDrawRect (d2, &term->pix_colors[bg >= 0 ? bg : Color_bg].c, 0, 0, w, h);
+            XftDrawRect (d2, &term->lookup_color(bg >= 0 ? bg : Color_bg, term->pix_colors).c, 0, 0, w, h);
 
-          XftDrawGlyphSpec (d2, &term->pix_colors[fg].c, f, enc, ep - enc);
+          XftDrawGlyphSpec (d2, &term->lookup_color(fg, term->pix_colors).c, f, enc, ep - enc);
           XCopyArea (disp, d2, d, gc, 0, 0, w, h, x, y);
         }
       else
@@ -1584,7 +1584,7 @@
   else
     {
       clear_rect (d, x, y, w, h, bg);
-      XftDrawGlyphSpec (d, &term->pix_colors[fg].c, f, enc, ep - enc);
+      XftDrawGlyphSpec (d, &term->lookup_color(fg, term->pix_colors).c, f, enc, ep - enc);
     }
 }
 
--- src/rxvtfont.h
+++ src/rxvtfont.h
@@ -72,8 +72,10 @@
   char *fontdesc;
 
   // must be power-of-two - 1, also has to match RS_fontMask in rxvt.h
-#if USE_256_COLORS
-  enum { fontCount =   7 }; // 2 extra colors bits, 2 fewer fontcount bits
+#if USE_24_BIT_COLOR
+  enum { fontCount =   7 }; // 36 extra colors bits, 2 fewer fontcount bits
+#elif USE_256_COLORS
+  enum { fontCount =   7 }; //  2 extra colors bits, 2 fewer fontcount bits
 #else
   enum { fontCount =  31 };
 #endif
--- src/rxvt.h
+++ src/rxvt.h
@@ -41,7 +41,11 @@
 #else
 typedef uint16_t text_t; // saves lots of memory
 #endif
+#if USE_24_BIT_COLOR
+typedef uint64_t rend_t;
+#else
 typedef uint32_t rend_t;
+#endif
 typedef  int32_t tlen_t;  // was int16_t, but this results in smaller code and memory use
 typedef  int32_t tlen_t_; // specifically for use in the line_t structure
 
@@ -351,46 +355,55 @@
 #define RS_None                 0
 
 // GET_BGATTR depends on RS_fgShift > RS_bgShift
-#define RS_colorMask		((1UL << Color_Bits) - 1UL)
-#define RS_bgShift		0
-#define RS_fgShift		(RS_bgShift + Color_Bits)
-#define RS_bgMask               (RS_colorMask << RS_bgShift)
-#define RS_fgMask               (RS_colorMask << RS_fgShift)
+#define RS_colorMask		    ((rend_t) ((1UL << Color_Bits) - 1UL))
+#define RS_bgShift		        0
+#define RS_fgShift		        (RS_bgShift + Color_Bits)
+#define RS_bgMask               ((rend_t) (RS_colorMask << RS_bgShift))
+#define RS_fgMask               ((rend_t) (RS_colorMask << RS_fgShift))
 
 // must have space for rxvt_fontset::fontCount * 2 + 2 values
 #define RS_fontShift            (RS_fgShift + Color_Bits)
-#define RS_Careful		(1UL << RS_fontShift)	/* be careful when drawing these */
-#define RS_fontCount		rxvt_fontset::fontCount
-#define RS_fontMask             ((RS_fontCount << (RS_fontShift + 1)) | RS_Careful)   // includes RS_Careful
+#define RS_Careful		        ((rend_t) (1UL << RS_fontShift))	/* be careful when drawing these */
+#define RS_fontCount		    ((rend_t) rxvt_fontset::fontCount)
+#define RS_fontMask             ((rend_t) ((RS_fontCount << (RS_fontShift + 1)) | RS_Careful))   // includes RS_Careful
 
 // toggle this to force redraw, must be != RS_Careful and otherwise "pretty neutral"
-#define RS_redraw		(2UL << RS_fontShift)
+#define RS_redraw		        ((rend_t) (2UL << RS_fontShift))
 
-#define RS_Sel                  (1UL << 22)
+#if USE_24_BIT_COLOR
+# define RS_fontCountSize 4
+#elif USE_256_COLORS
+# define RS_fontCountSize 4
+#else
+# define RS_fontCountSize 8
+#endif
+
+#define RS_selShift             (RS_fontShift + RS_fontCountSize)
+#define RS_Sel                  ((rend_t) (1UL << RS_selShift))
 
 // 4 custom bits for extensions
 #define RS_customCount          16UL
-#define RS_customShift          23
-#define RS_customMask           ((RS_customCount - 1UL) << RS_customShift)
+#define RS_customShift          (RS_selShift + 1)
+#define RS_customMask           ((rend_t) ((RS_customCount - 1UL) << RS_customShift))
 
 // font styles
-#define RS_Bold                 (1UL << RS_styleShift)
-#define RS_Italic		(2UL << RS_styleShift)
+#define RS_Bold                 ((rend_t) (1UL << RS_styleShift))
+#define RS_Italic		        ((rend_t) (2UL << RS_styleShift))
 
-#define RS_styleCount		4
-#define RS_styleShift		27
-#define RS_styleMask		(RS_Bold | RS_Italic)
+#define RS_styleCount		    4
+#define RS_styleShift	        (RS_customShift + RS_styleCount)
+#define RS_styleMask		    ((rend_t) (RS_Bold | RS_Italic))
 
 // fake styles
-#define RS_Blink                (1UL << 29)
-#define RS_RVid                 (1UL << 30)    // reverse video
-#define RS_Uline                (1UL << 31)    // underline
+#define RS_Blink                ((rend_t) (1UL << (RS_styleShift + 2)))
+#define RS_RVid                 ((rend_t) (1UL << (RS_styleShift + 3)))    // reverse video
+#define RS_Uline                ((rend_t) (1UL << (RS_styleShift + 4)))    // underline
 
-#define RS_baseattrMask         (RS_Italic | RS_Bold | RS_Blink | RS_RVid | RS_Uline)
-#define RS_attrMask             (RS_baseattrMask | RS_fontMask)
+#define RS_baseattrMask         ((rend_t) (RS_Italic | RS_Bold | RS_Blink | RS_RVid | RS_Uline))
+#define RS_attrMask             ((rend_t) (RS_baseattrMask | RS_fontMask))
 
 #define DEFAULT_RSTYLE  (RS_None | (Color_fg    << RS_fgShift) | (Color_bg     << RS_bgShift))
-#define OVERLAY_RSTYLE  (RS_None | (Color_Black << RS_fgShift) | (Color_Yellow << RS_bgShift))
+#define OVERLAY_RSTYLE  (RS_None | (Color_White << RS_fgShift) | (Color_Blue << RS_bgShift))
 
 enum {
   C0_NUL = 0x00,
@@ -544,7 +557,9 @@
 #endif
 };
 
-#if USE_256_COLORS
+#if USE_24_BIT_COLOR
+# define Color_Bits      25
+#elif USE_256_COLORS
 # define Color_Bits      9 // 0 .. maxTermCOLOR24
 #else
 # define Color_Bits      8 // 0 .. maxTermCOLOR24
@@ -680,21 +695,21 @@
 #define ROW(n) ROW_of (this, n)
 
 /* how to build & extract colors and attributes */
-#define GET_BASEFG(x)           (((x) & RS_fgMask) >> RS_fgShift)
-#define GET_BASEBG(x)           (((x) & RS_bgMask) >> RS_bgShift)
+#define GET_BASEFG(x)           ((((rend_t) (x)) & RS_fgMask) >> RS_fgShift)
+#define GET_BASEBG(x)           ((((rend_t) (x)) & RS_bgMask) >> RS_bgShift)
 
-#define GET_FONT(x)             (((x) & RS_fontMask) >> RS_fontShift)
-#define SET_FONT(x,fid)         (((x) & ~RS_fontMask) | ((fid) << RS_fontShift))
+#define GET_FONT(x)             ((((rend_t) (x)) & RS_fontMask) >> RS_fontShift)
+#define SET_FONT(x,fid)         ((((rend_t) (x)) & ~((rend_t) RS_fontMask)) | (((rend_t) (fid)) << RS_fontShift))
 
-#define GET_STYLE(x)		(((x) & RS_styleMask) >> RS_styleShift)
-#define SET_STYLE(x,style)	(((x) & ~RS_styleMask) | ((style) << RS_styleShift))
+#define GET_STYLE(x)		    ((((rend_t) (x)) & RS_styleMask) >> RS_styleShift)
+#define SET_STYLE(x,style)	    ((((rend_t) (x)) & ~((rend_t) RS_styleMask)) | (((rend_t) (style)) << RS_styleShift))
 
-#define GET_ATTR(x)             (((x) & RS_attrMask))
-#define SET_FGCOLOR(x,fg)       (((x) & ~RS_fgMask)   | ((fg) << RS_fgShift))
-#define SET_BGCOLOR(x,bg)       (((x) & ~RS_bgMask)   | ((bg) << RS_bgShift))
-#define SET_ATTR(x,a)           (((x) & ~RS_attrMask) | (a))
+#define GET_ATTR(x)             ((((rend_t) (x)) & RS_attrMask))
+#define SET_FGCOLOR(x,fg)       ((((rend_t) (x)) & ~((rend_t) RS_fgMask))   | (((rend_t) (fg)) << RS_fgShift))
+#define SET_BGCOLOR(x,bg)       ((((rend_t) (x)) & ~((rend_t) RS_bgMask))   | (((rend_t) (bg)) << RS_bgShift))
+#define SET_ATTR(x,a)           ((((rend_t) (x)) & ~((rend_t) RS_attrMask)) | ((rend_t) (a)))
 
-#define RS_SAME(a,b)		(!(((a) ^ (b)) & ~RS_Careful))
+#define RS_SAME(a,b)		    (!((((rend_t) (a)) ^ ((rend_t) (b))) & ~((rend_t) RS_Careful)))
 
 #define PIXCOLOR_NAME(idx)      rs[Rs_color + (idx)]
 #define ISSET_PIXCOLOR(idx)     (!!rs[Rs_color + (idx)])
@@ -1494,7 +1509,12 @@
   void scr_swap_screen () noexcept;
   void scr_change_screen (int scrn);
   void scr_color (unsigned int color, int fgbg) noexcept;
-  void scr_rendition (int set, int style) noexcept;
+#if USE_24_BIT_COLOR
+  void scr_color_24 (unsigned int color, int fgbg) noexcept;
+  void scr_color_rgb (unsigned int r, unsigned int g, unsigned int b, int fgbg) noexcept;
+#endif
+  rxvt_color &lookup_color (unsigned int color, rxvt_color *table) noexcept;
+  void scr_rendition (int set, rend_t style) noexcept;
   void scr_add_lines (const wchar_t *str, int len, int minlines = 0) noexcept;
   void scr_backspace () noexcept;
   void scr_tab (int count, bool ht = false) noexcept;
--- src/rxvtperl.xs
+++ src/rxvtperl.xs
@@ -802,6 +802,18 @@
 
 PROTOTYPES: ENABLE
 
+TYPEMAP: <<END
+rend_t T_UINT
+
+INPUT
+T_UINT
+  $var = ($type)SvUV($arg);
+
+OUTPUT
+T_UINT
+  sv_setuv($arg, (UV)$var);
+END
+
 BOOT:
 {
   sv_setsv (get_sv ("urxvt::LIBDIR",   1), newSVpvn (LIBDIR,   sizeof (LIBDIR)   - 1));
@@ -1128,43 +1140,43 @@
         OUTPUT:
         RETVAL
 
-int
-GET_BASEFG (int rend)
+rend_t
+GET_BASEFG (rend_t rend)
 	CODE:
         RETVAL = GET_BASEFG (rend);
 	OUTPUT:
         RETVAL
 
-int
-GET_BASEBG (int rend)
+rend_t
+GET_BASEBG (rend_t rend)
 	CODE:
         RETVAL = GET_BASEBG (rend);
 	OUTPUT:
         RETVAL
 
-int
-SET_FGCOLOR (int rend, int new_color)
+rend_t
+SET_FGCOLOR (rend_t rend, int new_color)
 	CODE:
         RETVAL = SET_FGCOLOR (rend, clamp (new_color, 0, TOTAL_COLORS - 1));
 	OUTPUT:
         RETVAL
 
-int
-SET_BGCOLOR (int rend, int new_color)
+rend_t
+SET_BGCOLOR (rend_t rend, int new_color)
 	CODE:
         RETVAL = SET_BGCOLOR (rend, clamp (new_color, 0, TOTAL_COLORS - 1));
 	OUTPUT:
         RETVAL
 
-int
-GET_CUSTOM (int rend)
+rend_t
+GET_CUSTOM (rend_t rend)
 	CODE:
         RETVAL = (rend & RS_customMask) >> RS_customShift;
 	OUTPUT:
         RETVAL
 
-int
-SET_CUSTOM (int rend, int new_value)
+rend_t
+SET_CUSTOM (rend_t rend, int new_value)
 	CODE:
 {
         if (!IN_RANGE_EXC (new_value, 0, RS_customCount))
@@ -1619,8 +1631,8 @@
         THIS->vt_emask_perl |= emask;
         THIS->vt_select_input ();
 
-U32
-rxvt_term::rstyle (U32 new_rstyle = THIS->rstyle)
+UV
+rxvt_term::rstyle (rend_t new_rstyle = THIS->rstyle)
 	CODE:
         RETVAL = THIS->rstyle;
         THIS->rstyle = new_rstyle;
@@ -2106,10 +2118,10 @@
         RETVAL
 
 void
-rxvt_term::scr_xor_rect (int beg_row, int beg_col, int end_row, int end_col, U32 rstyle1 = RS_RVid, U32 rstyle2 = RS_RVid | RS_Uline)
+rxvt_term::scr_xor_rect (int beg_row, int beg_col, int end_row, int end_col, rend_t rstyle1 = RS_RVid, rend_t rstyle2 = RS_RVid | RS_Uline)
 
 void
-rxvt_term::scr_xor_span (int beg_row, int beg_col, int end_row, int end_col, U32 rstyle = RS_RVid)
+rxvt_term::scr_xor_span (int beg_row, int beg_col, int end_row, int end_col, rend_t rstyle = RS_RVid)
 
 void
 rxvt_term::scr_bell ()
@@ -2175,7 +2187,7 @@
 }
 
 SV *
-rxvt_term::overlay (int x, int y, int w, int h, int rstyle = OVERLAY_RSTYLE, int border = 2)
+rxvt_term::overlay (int x, int y, int w, int h, rend_t rstyle = OVERLAY_RSTYLE, int border = 2)
 	CODE:
 {
         overlay *o = new overlay (THIS, x, y, w, h, rstyle, border);
@@ -2297,8 +2309,8 @@
 XCreateSimpleWindow (rxvt_term *term, Window parent, int x, int y, unsigned int width, unsigned int height)
 	C_ARGS: term->dpy, (Window)parent,
                 x, y, width, height, 0,
-                term->pix_colors_focused[Color_border],
-                term->pix_colors_focused[Color_border]
+                term->lookup_color(Color_border, term->pix_colors_focused),
+                term->lookup_color(Color_border, term->pix_colors_focused)
 
 #endif
 
@@ -2432,7 +2444,7 @@
             img->reify ()
                ->replace (img);
 
-            img->convert_format (XRenderFindVisualFormat (THIS->dpy, THIS->visual), THIS->pix_colors [Color_bg])
+            img->convert_format (XRenderFindVisualFormat (THIS->dpy, THIS->visual), THIS->lookup_color(Color_bg, THIS->pix_colors))
                ->replace (img);
 
             THIS->bg_img = img;
--- src/screen.C
+++ src/screen.C
@@ -627,12 +627,51 @@
     rstyle = SET_BGCOLOR (rstyle, color);
 }
 
+#if USE_24_BIT_COLOR
+static rxvt_color *scr_colors[1 << 24];
+
+void
+rxvt_term::scr_color_24 (unsigned int color, int fgbg) noexcept
+{
+  color += TOTAL_COLORS;
+  if (fgbg == Color_fg)
+    rstyle = SET_FGCOLOR (rstyle, color);
+  else
+    rstyle = SET_BGCOLOR (rstyle, color);
+}
+
+void
+rxvt_term::scr_color_rgb (unsigned int r, unsigned int g, unsigned int b, int fgbg) noexcept
+{
+  unsigned int color = (r << 16) + (g << 8) + b;
+  scr_color_24(color, fgbg);
+}
+#endif
+
+rxvt_color
+&rxvt_term::lookup_color (unsigned int color, rxvt_color *table) noexcept
+{
+#if USE_24_BIT_COLOR
+  if (color >= TOTAL_COLORS) {
+    color -= TOTAL_COLORS;
+    if (scr_colors[color] == NULL) {
+        scr_colors[color] = new rxvt_color();
+        char name[1+2*3+1];
+        sprintf(name, "#%02x%02x%02x", (color >> 16) & 0xff, (color >> 8) & 0xff, color & 0xff);
+        scr_colors[color]->set(this, name);
+    }
+    return *scr_colors[color];
+  } else
+#endif
+  return table[color];
+}
+
 /* ------------------------------------------------------------------------- */
 /*
  * Change the rendition style for following text
  */
 void
-rxvt_term::scr_rendition (int set, int style) noexcept
+rxvt_term::scr_rendition (int set, rend_t style) noexcept
 {
   if (set)
     rstyle |= style;
@@ -1390,13 +1429,13 @@
 
       if (mapped)
         {
-          gcvalue.foreground = pix_colors[bgcolor_of (rstyle)];
+          gcvalue.foreground = lookup_color(bgcolor_of (rstyle), pix_colors);
           XChangeGC (dpy, gc, GCForeground, &gcvalue);
           XFillRectangle (dpy, vt, gc,
                           0, Row2Pixel (row - view_start),
                           (unsigned int)vt_width,
                           (unsigned int)Height2Pixel (num));
-          gcvalue.foreground = pix_colors[Color_fg];
+          gcvalue.foreground = lookup_color(Color_fg, pix_colors);
           XChangeGC (dpy, gc, GCForeground, &gcvalue);
         }
     }
@@ -1725,15 +1764,15 @@
         }
       else
 #endif
-      ::swap (pix_colors[Color_fg], pix_colors[Color_bg]);
+      ::swap (lookup_color(Color_fg, pix_colors), lookup_color(Color_bg, pix_colors));
 #ifdef HAVE_IMG
       if (bg_img == 0)
 #endif
-          XSetWindowBackground (dpy, vt, pix_colors[Color_bg]);
+          XSetWindowBackground (dpy, vt, lookup_color(Color_bg, pix_colors));
 
       XGCValues gcvalue;
-      gcvalue.foreground = pix_colors[Color_fg];
-      gcvalue.background = pix_colors[Color_bg];
+      gcvalue.foreground = lookup_color(Color_fg, pix_colors);
+      gcvalue.background = lookup_color(Color_bg, pix_colors);
       XChangeGC (dpy, gc, GCBackground | GCForeground, &gcvalue);
 
       scr_clear ();
@@ -2431,14 +2470,14 @@
             {
               if (showcursor && focus && row == screen.cur.row
                   && IN_RANGE_EXC (col, cur_col, cur_col + cursorwidth))
-                XSetForeground (dpy, gc, pix_colors[ccol1]);
+                XSetForeground (dpy, gc, lookup_color(ccol1, pix_colors));
               else
 #if ENABLE_FRILLS
               if (ISSET_PIXCOLOR (Color_underline))
-                XSetForeground (dpy, gc, pix_colors[Color_underline]);
+                XSetForeground (dpy, gc, lookup_color(Color_underline, pix_colors));
               else
 #endif
-                XSetForeground (dpy, gc, pix_colors[fore]);
+                XSetForeground (dpy, gc, lookup_color(fore, pix_colors));
 
               XDrawLine (dpy, vt, gc,
                          xpixel, ypixel + font->ascent + 1,
@@ -2458,7 +2497,7 @@
             scr_set_char_rend (ROW(screen.cur.row), cur_col, cur_rend);
           else if (oldcursor.row >= 0)
             {
-              XSetForeground (dpy, gc, pix_colors[ccol1]);
+              XSetForeground (dpy, gc, lookup_color(ccol1, pix_colors));
               if (cursor_type == 1)
                 XFillRectangle (dpy, vt, gc,
                                 Col2Pixel (cur_col),
@@ -2475,7 +2514,7 @@
         }
       else if (oldcursor.row >= 0)
         {
-          XSetForeground (dpy, gc, pix_colors[ccol1]);
+          XSetForeground (dpy, gc, lookup_color(ccol1, pix_colors));
 
           XDrawRectangle (dpy, vt, gc,
                           Col2Pixel (cur_col),
@@ -2544,15 +2583,15 @@
       else
 # endif
         {
-          XSetWindowBackground (dpy, parent, pix_colors[Color_border]);
+          XSetWindowBackground (dpy, parent, lookup_color(Color_border, pix_colors));
           XSetWindowBackgroundPixmap (dpy, vt, bg_img->pm);
         }
     }
   else
 #endif
     {
-      XSetWindowBackground (dpy, parent, pix_colors[Color_border]);
-      XSetWindowBackground (dpy, vt, pix_colors[Color_bg]);
+      XSetWindowBackground (dpy, parent, lookup_color(Color_border, pix_colors));
+      XSetWindowBackground (dpy, vt, lookup_color(Color_bg, pix_colors));
     }
 
   XClearWindow (dpy, parent);
@@ -2562,7 +2601,7 @@
       if (transparent)
         XSetWindowBackgroundPixmap (dpy, scrollBar.win, ParentRelative);
       else
-        XSetWindowBackground (dpy, scrollBar.win, pix_colors[scrollBar.color ()]);
+        XSetWindowBackground (dpy, scrollBar.win, lookup_color(scrollBar.color (), pix_colors));
       scrollBar.state = SB_STATE_IDLE;
       scrollBar.show (0);
     }
--- src/scrollbar.C
+++ src/scrollbar.C
@@ -72,8 +72,8 @@
                                  total_width (),
                                  term->szHint.height,
                                  0,
-                                 term->pix_colors[Color_fg],
-                                 term->pix_colors[color ()]);
+                                 term->lookup_color(Color_fg, term->pix_colors),
+                                 term->lookup_color(color (), term->pix_colors));
       XDefineCursor (term->dpy, win, leftptr_cursor);
 
       XSelectInput (term->dpy, win,
--- src/scrollbar-next.C
+++ src/scrollbar-next.C
@@ -154,15 +154,15 @@
 
   gcvalue.graphics_exposures = False;
 
-  gcvalue.foreground = term->pix_colors_focused[Color_Black];
+  gcvalue.foreground = term->lookup_color(Color_Black, term->pix_colors_focused);
   blackGC = XCreateGC (term->dpy, win,
                        GCForeground | GCGraphicsExposures, &gcvalue);
 
-  gcvalue.foreground = term->pix_colors_focused[Color_White];
+  gcvalue.foreground = term->lookup_color(Color_White, term->pix_colors_focused);
   whiteGC = XCreateGC (term->dpy, win,
                        GCForeground | GCGraphicsExposures, &gcvalue);
 
-  light = term->pix_colors_focused[Color_scroll];
+  light = term->lookup_color(Color_scroll, term->pix_colors_focused);
 #if 0
   //color used by rxvt
   if (color.set (term, rgba (0xaeba, 0xaaaa, 0xaeba)))
@@ -172,7 +172,7 @@
   grayGC = XCreateGC (term->dpy, win,
                       GCForeground | GCGraphicsExposures, &gcvalue);
 
-  dark = term->pix_colors_focused[Color_Grey25];
+  dark = term->lookup_color(Color_Grey25, term->pix_colors_focused);
 #if 0
   //color used by rxvt
   if (color.set (term, rgba (0x51aa, 0x5555, 0x5144)))
--- src/scrollbar-plain.C
+++ src/scrollbar-plain.C
@@ -38,7 +38,7 @@
       XGCValues gcvalue;
 
       init |= SB_STYLE_PLAIN;
-      gcvalue.foreground = term->pix_colors_focused[Color_scroll];
+      gcvalue.foreground = term->lookup_color(Color_scroll, term->pix_colors_focused);
 
       pscrollbarGC = XCreateGC (term->dpy, win, GCForeground, &gcvalue);
     }
--- src/scrollbar-rxvt.C
+++ src/scrollbar-rxvt.C
@@ -158,11 +158,11 @@
 
       init |= SB_STYLE_RXVT;
 
-      gcvalue.foreground = term->pix_colors[Color_topShadow];
+      gcvalue.foreground = term->lookup_color(Color_topShadow, term->pix_colors);
       topShadowGC = XCreateGC (term->dpy, term->vt, GCForeground, &gcvalue);
-      gcvalue.foreground = term->pix_colors[Color_bottomShadow];
+      gcvalue.foreground = term->lookup_color(Color_bottomShadow, term->pix_colors);
       botShadowGC = XCreateGC (term->dpy, term->vt, GCForeground, &gcvalue);
-      gcvalue.foreground = term->pix_colors[ (term->depth <= 2 ? Color_fg : Color_scroll)];
+      gcvalue.foreground = term->lookup_color( (term->depth <= 2 ? Color_fg : Color_scroll), term->pix_colors);
       scrollbarGC = XCreateGC (term->dpy, term->vt, GCForeground, &gcvalue);
     }
   else
--- src/scrollbar-xterm.C
+++ src/scrollbar-xterm.C
@@ -49,13 +49,13 @@
         rxvt_fatal ("can't create bitmap\n");
 
       gcvalue.fill_style = FillOpaqueStippled;
-      gcvalue.foreground = term->pix_colors_focused[Color_scroll];
-      gcvalue.background = term->pix_colors_focused[Color_bg];
+      gcvalue.foreground = term->lookup_color(Color_scroll, term->pix_colors_focused);
+      gcvalue.background = term->lookup_color(Color_bg, term->pix_colors_focused);
 
       xscrollbarGC = XCreateGC (term->dpy, win,
                                 GCForeground | GCBackground
                                 | GCFillStyle | GCStipple, &gcvalue);
-      gcvalue.foreground = term->pix_colors_focused[Color_border];
+      gcvalue.foreground = term->lookup_color(Color_border, term->pix_colors_focused);
       ShadowGC = XCreateGC (term->dpy, win, GCForeground, &gcvalue);
     }