… | |
… | |
23 | |
23 | |
24 | #include "../config.h" |
24 | #include "../config.h" |
25 | #include <rxvt.h> |
25 | #include <rxvt.h> |
26 | #include <rxvttoolkit.h> |
26 | #include <rxvttoolkit.h> |
27 | |
27 | |
|
|
28 | #include <stdlib.h> |
|
|
29 | |
28 | #include <unistd.h> |
30 | #include <unistd.h> |
29 | #include <fcntl.h> |
31 | #include <fcntl.h> |
30 | |
32 | |
31 | #include <sys/utsname.h> |
33 | #include <sys/utsname.h> |
32 | #include <sys/socket.h> |
34 | #include <sys/socket.h> |
… | |
… | |
753 | unsigned int alpha = color.a >= 0xff00 ? 0xffff : color.a; |
755 | unsigned int alpha = color.a >= 0xff00 ? 0xffff : color.a; |
754 | |
756 | |
755 | #if XFT |
757 | #if XFT |
756 | XRenderPictFormat *format; |
758 | XRenderPictFormat *format; |
757 | |
759 | |
|
|
760 | // not needed by XftColorAlloc, but by the other paths (ours |
|
|
761 | // and fallback), so just set all components here. |
|
|
762 | c.color.red = color.r; |
|
|
763 | c.color.green = color.g; |
|
|
764 | c.color.blue = color.b; |
|
|
765 | c.color.alpha = alpha; |
|
|
766 | |
758 | // FUCKING Xft gets it wrong, of course, so work around it. |
767 | // FUCKING Xft gets it wrong, of course, so work around it. |
759 | // Transparency users should eat shit and die, and then |
768 | // Transparency users should eat shit and die, and then |
760 | // XRenderQueryPictIndexValues themselves plenty. |
769 | // XRenderQueryPictIndexValues themselves plenty. |
761 | if ((screen->visual->c_class == TrueColor) |
770 | if ((screen->visual->c_class == TrueColor) |
762 | && (format = XRenderFindVisualFormat (screen->dpy, screen->visual))) |
771 | && (format = XRenderFindVisualFormat (screen->dpy, screen->visual))) |
763 | { |
772 | { |
764 | // the fun lies in doing everything manually... |
773 | // the fun lies in doing everything manually... |
765 | c.color.red = color.r; |
|
|
766 | c.color.green = color.g; |
|
|
767 | c.color.blue = color.b; |
|
|
768 | c.color.alpha = alpha; |
|
|
769 | |
774 | |
770 | // Xft wants premultiplied alpha, but abuses the alpha channel |
775 | // Xft wants premultiplied alpha, but abuses the alpha channel |
771 | // as blend factor, and doesn't allow us to set the alpha channel |
776 | // as blend factor, and doesn't allow us to set the alpha channel |
772 | c.color.red = c.color.red * alpha / 0xffff; |
777 | c.color.red = c.color.red * alpha / 0xffff; |
773 | c.color.green = c.color.green * alpha / 0xffff; |
778 | c.color.green = c.color.green * alpha / 0xffff; |
… | |
… | |
787 | d.red = color.r; |
792 | d.red = color.r; |
788 | d.green = color.g; |
793 | d.green = color.g; |
789 | d.blue = color.b; |
794 | d.blue = color.b; |
790 | d.alpha = alpha; |
795 | d.alpha = alpha; |
791 | |
796 | |
|
|
797 | // XftColorAlloc always returns 100% transparent pixels(!) |
792 | if (XftColorAllocValue (screen->dpy, screen->visual, screen->cmap, &d, &c)) |
798 | if (XftColorAllocValue (screen->dpy, screen->visual, screen->cmap, &d, &c)) |
793 | return true; |
799 | return true; |
794 | } |
800 | } |
795 | #else |
801 | #else |
796 | c.red = color.r; |
802 | c.red = color.r; |
… | |
… | |
807 | } |
813 | } |
808 | else if (XAllocColor (screen->dpy, screen->cmap, &c)) |
814 | else if (XAllocColor (screen->dpy, screen->cmap, &c)) |
809 | return true; |
815 | return true; |
810 | #endif |
816 | #endif |
811 | |
817 | |
812 | c.pixel = (color.r + color.g + color.b) > 128*3 |
818 | c.pixel = (color.r * 2 + color.g * 3 + color.b) >= 0x8000 * 6 |
813 | ? WhitePixelOfScreen (DefaultScreenOfDisplay (screen->dpy)) |
819 | ? WhitePixelOfScreen (DefaultScreenOfDisplay (screen->dpy)) |
814 | : BlackPixelOfScreen (DefaultScreenOfDisplay (screen->dpy)); |
820 | : BlackPixelOfScreen (DefaultScreenOfDisplay (screen->dpy)); |
815 | |
821 | |
816 | return false; |
822 | return false; |
817 | } |
823 | } |
… | |
… | |
819 | bool |
825 | bool |
820 | rxvt_color::set (rxvt_screen *screen, const char *name) |
826 | rxvt_color::set (rxvt_screen *screen, const char *name) |
821 | { |
827 | { |
822 | rgba c; |
828 | rgba c; |
823 | char eos; |
829 | char eos; |
824 | int skip; |
830 | int skip = 0; |
825 | |
831 | |
826 | c.a = rgba::MAX_CC; |
832 | c.a = rgba::MAX_CC; |
827 | |
833 | |
828 | // parse the nonstandard "[alphapercent]" prefix |
834 | // parse the nonstandard "[alphapercent]" prefix |
829 | if (1 <= sscanf (name, "[%hd]%n", &c.a, &skip)) |
835 | if (1 <= sscanf (name, "[%hd]%n", &c.a, &skip)) |
… | |
… | |
877 | // many extra optimisations. |
883 | // many extra optimisations. |
878 | XQueryColors (screen->dpy, screen->cmap, colors, cmap_size); |
884 | XQueryColors (screen->dpy, screen->cmap, colors, cmap_size); |
879 | |
885 | |
880 | while (cmap_size) |
886 | while (cmap_size) |
881 | { |
887 | { |
882 | int diff = 0x7fffffffUL; |
888 | int diff = 0x7fffffffL; |
883 | XColor *best = colors; |
889 | XColor *best = colors; |
884 | |
890 | |
885 | for (int i = 0; i < cmap_size; i++) |
891 | for (int i = 0; i < cmap_size; i++) |
886 | { |
892 | { |
|
|
893 | // simple weighted rgb distance sucks, but keeps it simple |
887 | int d = (squared_diff<int> (color.r >> 2, colors [i].red >> 2)) |
894 | int d = abs (color.r - colors [i].red ) * 2 |
888 | + (squared_diff<int> (color.g >> 2, colors [i].green >> 2)) |
895 | + abs (color.g - colors [i].green) * 3 |
889 | + (squared_diff<int> (color.b >> 2, colors [i].blue >> 2)); |
896 | + abs (color.b - colors [i].blue ); |
890 | |
897 | |
891 | if (d < diff) |
898 | if (d < diff) |
892 | { |
899 | { |
893 | diff = d; |
900 | diff = d; |
894 | best = colors + i; |
901 | best = colors + i; |
… | |
… | |
915 | |
922 | |
916 | void |
923 | void |
917 | rxvt_color::get (rgba &color) const |
924 | rxvt_color::get (rgba &color) const |
918 | { |
925 | { |
919 | #if XFT |
926 | #if XFT |
|
|
927 | |
920 | color.r = c.color.red; |
928 | color.r = c.color.red; |
921 | color.g = c.color.green; |
929 | color.g = c.color.green; |
922 | color.b = c.color.blue; |
930 | color.b = c.color.blue; |
923 | color.a = c.color.alpha; |
931 | color.a = c.color.alpha; |
|
|
932 | |
|
|
933 | if (IN_RANGE_INC (color.a, 0x0001, 0xfffe)) |
|
|
934 | { |
|
|
935 | color.r = color.r * 0xffff / color.a; |
|
|
936 | color.g = color.g * 0xffff / color.a; |
|
|
937 | color.b = color.b * 0xffff / color.a; |
|
|
938 | } |
|
|
939 | |
924 | #else |
940 | #else |
|
|
941 | |
925 | color.r = c.red; |
942 | color.r = c.red; |
926 | color.g = c.green; |
943 | color.g = c.green; |
927 | color.b = c.blue; |
944 | color.b = c.blue; |
928 | color.a = rgba::MAX_CC; |
945 | color.a = rgba::MAX_CC; |
|
|
946 | |
929 | #endif |
947 | #endif |
930 | } |
948 | } |
931 | |
949 | |
932 | void |
950 | void |
933 | rxvt_color::get (XColor &color) const |
951 | rxvt_color::get (XColor &color) const |