… | |
… | |
560 | refcache<rxvt_display> displays; |
560 | refcache<rxvt_display> displays; |
561 | |
561 | |
562 | ///////////////////////////////////////////////////////////////////////////// |
562 | ///////////////////////////////////////////////////////////////////////////// |
563 | |
563 | |
564 | bool |
564 | bool |
565 | rxvt_color::alloc (rxvt_screen *screen, rxvt_rgba rgba) |
565 | rxvt_color::alloc (rxvt_screen *screen, const rgba &color) |
566 | { |
566 | { |
567 | #if XFT |
567 | #if XFT |
568 | XRenderPictFormat *format; |
568 | XRenderPictFormat *format; |
569 | |
569 | |
570 | // FUCKING Xft gets it wrong, of course, so work around it |
570 | // FUCKING Xft gets it wrong, of course, so work around it |
… | |
… | |
572 | // XRenderQueryPictIndexValues themselves plenty. |
572 | // XRenderQueryPictIndexValues themselves plenty. |
573 | if ((screen->visual->c_class == TrueColor || screen->visual->c_class == DirectColor) |
573 | if ((screen->visual->c_class == TrueColor || screen->visual->c_class == DirectColor) |
574 | && (format = XRenderFindVisualFormat (screen->xdisp, screen->visual))) |
574 | && (format = XRenderFindVisualFormat (screen->xdisp, screen->visual))) |
575 | { |
575 | { |
576 | // the fun lies in doing everything manually... |
576 | // the fun lies in doing everything manually... |
577 | c.color.red = rgba.r; |
577 | c.color.red = color.r; |
578 | c.color.green = rgba.g; |
578 | c.color.green = color.g; |
579 | c.color.blue = rgba.b; |
579 | c.color.blue = color.b; |
580 | c.color.alpha = rgba.a; |
580 | c.color.alpha = color.a; |
581 | |
581 | |
582 | c.pixel = ((rgba.r * format->direct.redMask / rxvt_rgba::MAX_CC) << format->direct.red ) |
582 | c.pixel = ((color.r * format->direct.redMask / rgba::MAX_CC) << format->direct.red ) |
583 | | ((rgba.g * format->direct.greenMask / rxvt_rgba::MAX_CC) << format->direct.green) |
583 | | ((color.g * format->direct.greenMask / rgba::MAX_CC) << format->direct.green) |
584 | | ((rgba.b * format->direct.blueMask / rxvt_rgba::MAX_CC) << format->direct.blue ) |
584 | | ((color.b * format->direct.blueMask / rgba::MAX_CC) << format->direct.blue ) |
585 | | ((rgba.a * format->direct.alphaMask / rxvt_rgba::MAX_CC) << format->direct.alpha); |
585 | | ((color.a * format->direct.alphaMask / rgba::MAX_CC) << format->direct.alpha); |
586 | |
586 | |
587 | return true; |
587 | return true; |
588 | } |
588 | } |
589 | else |
589 | else |
590 | { |
590 | { |
591 | XRenderColor d; |
591 | XRenderColor d; |
592 | |
592 | |
593 | d.red = rgba.r; |
593 | d.red = color.r; |
594 | d.green = rgba.g; |
594 | d.green = color.g; |
595 | d.blue = rgba.b; |
595 | d.blue = color.b; |
596 | d.alpha = rgba.a; |
596 | d.alpha = color.a; |
597 | |
597 | |
598 | return XftColorAllocValue (screen->xdisp, screen->visual, screen->cmap, &d, &c); |
598 | return XftColorAllocValue (screen->xdisp, screen->visual, screen->cmap, &d, &c); |
599 | } |
599 | } |
600 | #else |
600 | #else |
601 | if (screen->visual->c_class == TrueColor || screen->visual->c_class == DirectColor) |
601 | if (screen->visual->c_class == TrueColor || screen->visual->c_class == DirectColor) |
602 | { |
602 | { |
|
|
603 | c.red = color.g; |
|
|
604 | c.green = color.g; |
|
|
605 | c.blue = color.g; |
603 | p = (rgba.r * (screen->visual->red_mask >> ctz (screen->visual->red_mask )) |
606 | c.pixel = (color.r * (screen->visual->red_mask >> ctz (screen->visual->red_mask )) |
604 | / rxvt_rgba::MAX_CC) << ctz (screen->visual->red_mask ) |
607 | / rgba::MAX_CC) << ctz (screen->visual->red_mask ) |
605 | | (rgba.g * (screen->visual->green_mask >> ctz (screen->visual->green_mask)) |
608 | | (color.g * (screen->visual->green_mask >> ctz (screen->visual->green_mask)) |
606 | / rxvt_rgba::MAX_CC) << ctz (screen->visual->green_mask) |
609 | / rgba::MAX_CC) << ctz (screen->visual->green_mask) |
607 | | (rgba.b * (screen->visual->blue_mask >> ctz (screen->visual->blue_mask )) |
610 | | (color.b * (screen->visual->blue_mask >> ctz (screen->visual->blue_mask )) |
608 | / rxvt_rgba::MAX_CC) << ctz (screen->visual->blue_mask ); |
611 | / rgba::MAX_CC) << ctz (screen->visual->blue_mask ); |
609 | |
612 | |
610 | return true; |
613 | return true; |
611 | } |
614 | } |
612 | else |
615 | else |
613 | { |
616 | { |
614 | XColor xc; |
|
|
615 | |
|
|
616 | xc.red = rgba.r; |
617 | c.red = color.r; |
617 | xc.green = rgba.g; |
618 | c.green = color.g; |
618 | xc.blue = rgba.b; |
619 | c.blue = color.b; |
619 | |
620 | |
620 | if (XAllocColor (screen->xdisp, screen->cmap, &xc)) |
621 | if (XAllocColor (screen->xdisp, screen->cmap, &c)) |
621 | { |
|
|
622 | p = xc.pixel; |
|
|
623 | return true; |
622 | return true; |
624 | } |
|
|
625 | else |
623 | else |
626 | p = (rgba.r + rgba.g + rgba.b) > 128*3 |
624 | c.pixel = (color.r + color.g + color.b) > 128*3 |
627 | ? WhitePixelOfScreen (DefaultScreenOfDisplay (screen->xdisp)) |
625 | ? WhitePixelOfScreen (DefaultScreenOfDisplay (screen->xdisp)) |
628 | : BlackPixelOfScreen (DefaultScreenOfDisplay (screen->xdisp)); |
626 | : BlackPixelOfScreen (DefaultScreenOfDisplay (screen->xdisp)); |
629 | } |
627 | } |
630 | #endif |
628 | #endif |
631 | |
629 | |
632 | return false; |
630 | return false; |
633 | } |
631 | } |
634 | |
632 | |
635 | bool |
633 | bool |
636 | rxvt_color::set (rxvt_screen *screen, const char *name) |
634 | rxvt_color::set (rxvt_screen *screen, const char *name) |
637 | { |
635 | { |
638 | int l = strlen (name); |
636 | rgba c; |
639 | rxvt_rgba r; |
|
|
640 | char eos; |
637 | char eos; |
641 | int mult; |
638 | int skip; |
642 | XColor xc, xc_exact; |
|
|
643 | |
639 | |
644 | // parse a number of non-standard ARGB colour specifications |
640 | if (1 <= sscanf (name, "[%hd]%n", &c.a, &skip)) |
645 | if ( l == 1+4*1 && 4 == sscanf (name, "#%1hx%1hx%1hx%1hx%c", &r.a, &r.r, &r.g, &r.b, &eos)) |
|
|
646 | mult = rxvt_rgba::MAX_CC / 0x0010; |
|
|
647 | else if (l == 1+4*2 && 4 == sscanf (name, "#%2hx%2hx%2hx%2hx%c", &r.a, &r.r, &r.g, &r.b, &eos)) |
|
|
648 | mult = rxvt_rgba::MAX_CC / 0x0100; |
|
|
649 | else if (l == 1+4*4 && 4 == sscanf (name, "#%4hx%4hx%4hx%4hx%c", &r.a, &r.r, &r.g, &r.b, &eos)) |
|
|
650 | mult = rxvt_rgba::MAX_CC / 0x0100; |
|
|
651 | else if (l == 4+5*4 && 4 == sscanf (name, "rgba:%hx/%hx/%hx/%hx%c", &r.r, &r.g, &r.b, &r.a, &eos)) |
|
|
652 | mult = rxvt_rgba::MAX_CC / 0xffff; |
|
|
653 | else if (XParseColor (screen->xdisp, screen->cmap, name, &xc)) |
|
|
654 | { |
641 | { |
655 | r.r = xc.red; |
642 | printf ("X %d\n", c.a); |
656 | r.g = xc.green; |
643 | c.a = lerp<int, int, int> (0, rgba::MAX_CC, c.a); |
657 | r.b = xc.blue; |
644 | name += skip; |
658 | mult = rxvt_rgba::MAX_CC / 0xffff; |
645 | printf ("y %04x\n", c.a); |
659 | } |
646 | } |
660 | else |
647 | else |
|
|
648 | c.a = rgba::MAX_CC; |
|
|
649 | |
|
|
650 | // parse the non-standard rgba format |
|
|
651 | if (strlen (name) != 4+5*4 || 4 != sscanf (name, "rgba:%hx/%hx/%hx/%hx%c", &c.r, &c.g, &c.b, &c.a, &eos)) |
661 | { |
652 | { |
|
|
653 | XColor xc, xc_exact; |
|
|
654 | |
|
|
655 | if (XParseColor (screen->xdisp, screen->cmap, name, &xc)) |
|
|
656 | { |
|
|
657 | c.r = xc.red; |
|
|
658 | c.g = xc.green; |
|
|
659 | c.b = xc.blue; |
|
|
660 | } |
|
|
661 | else |
|
|
662 | { |
|
|
663 | c.r = 0xffff; |
|
|
664 | c.g = 0x6969; |
|
|
665 | c.b = 0xb4b4; |
|
|
666 | |
662 | rxvt_warn ("failed to allocate color '%s', using pink instead.\n", name); |
667 | rxvt_warn ("unable to parse color '%s', using pink instead.\n", name); |
663 | r.r = 255; |
668 | } |
664 | r.g = 105; |
|
|
665 | r.b = 180; |
|
|
666 | mult = rxvt_rgba::MAX_CC / 0x00ff; |
|
|
667 | } |
669 | } |
668 | |
670 | |
669 | r.r *= mult; r.g *= mult; r.b *= mult; r.a *= mult; |
|
|
670 | |
|
|
671 | return set (screen, r); |
671 | return set (screen, c); |
672 | } |
672 | } |
673 | |
673 | |
674 | bool |
674 | bool |
675 | rxvt_color::set (rxvt_screen *screen, rxvt_rgba rgba) |
675 | rxvt_color::set (rxvt_screen *screen, const rgba &color) |
676 | { |
676 | { |
677 | bool got = alloc (screen, rgba); |
677 | bool got = alloc (screen, color); |
678 | |
678 | |
679 | #if !ENABLE_MINIMAL |
679 | #if !ENABLE_MINIMAL |
680 | int cmap_size = screen->visual->map_entries; |
680 | int cmap_size = screen->visual->map_entries; |
681 | |
681 | |
682 | if (!got |
682 | if (!got |
… | |
… | |
686 | XColor *colors = new XColor [screen->visual->map_entries]; |
686 | XColor *colors = new XColor [screen->visual->map_entries]; |
687 | |
687 | |
688 | for (int i = 0; i < cmap_size; i++) |
688 | for (int i = 0; i < cmap_size; i++) |
689 | colors [i].pixel = i; |
689 | colors [i].pixel = i; |
690 | |
690 | |
|
|
691 | // many kilobytes transfer per colour, but pseudocolor isn't worth |
|
|
692 | // many extra optimisations. |
691 | XQueryColors (screen->xdisp, screen->cmap, colors, cmap_size); |
693 | XQueryColors (screen->xdisp, screen->cmap, colors, cmap_size); |
692 | |
694 | |
693 | int diff = 0x7fffffffUL; |
695 | int diff = 0x7fffffffUL; |
694 | XColor *best = colors; |
696 | XColor *best = colors; |
695 | |
697 | |
696 | for (int i = 0; i < cmap_size; i++) |
698 | for (int i = 0; i < cmap_size; i++) |
697 | { |
699 | { |
698 | int d = (squared_diff<int> (rgba.r >> 2, colors [i].red >> 2)) |
700 | int d = (squared_diff<int> (color.r >> 2, colors [i].red >> 2)) |
699 | + (squared_diff<int> (rgba.g >> 2, colors [i].green >> 2)) |
701 | + (squared_diff<int> (color.g >> 2, colors [i].green >> 2)) |
700 | + (squared_diff<int> (rgba.b >> 2, colors [i].blue >> 2)); |
702 | + (squared_diff<int> (color.b >> 2, colors [i].blue >> 2)); |
701 | |
703 | |
702 | if (d < diff) |
704 | if (d < diff) |
703 | { |
705 | { |
704 | diff = d; |
706 | diff = d; |
705 | best = colors + i; |
707 | best = colors + i; |
706 | } |
708 | } |
707 | } |
709 | } |
708 | |
710 | |
709 | //rxvt_warn ("could not allocate %04x %04x %04x, getting %04x %04x %04x instead (%d)\n", |
711 | //rxvt_warn ("could not allocate %04x %04x %04x, getting %04x %04x %04x instead (%d)\n", |
710 | // rgba.r, rgba.g, rgba.b, best->red, best->green, best->blue, diff); |
712 | // color.r, color.g, color.b, best->red, best->green, best->blue, diff); |
711 | |
713 | |
712 | got = alloc (screen, rxvt_rgba (best->red, best->green, best->blue)); |
714 | got = alloc (screen, rgba (best->red, best->green, best->blue)); |
713 | |
715 | |
714 | delete colors; |
716 | delete colors; |
715 | } |
717 | } |
716 | #endif |
718 | #endif |
717 | |
719 | |
718 | return got; |
720 | return got; |
719 | } |
721 | } |
720 | |
722 | |
721 | void |
723 | void |
722 | rxvt_color::get (rxvt_screen *screen, rxvt_rgba &rgba) |
724 | rxvt_color::get (rxvt_screen *screen, rgba &color) |
723 | { |
725 | { |
724 | #if XFT |
726 | #if XFT |
725 | rgba.r = c.color.red; |
727 | color.r = c.color.red; |
726 | rgba.g = c.color.green; |
728 | color.g = c.color.green; |
727 | rgba.b = c.color.blue; |
729 | color.b = c.color.blue; |
728 | rgba.a = c.color.alpha; |
730 | color.a = c.color.alpha; |
729 | #else |
731 | #else |
730 | XColor c; |
|
|
731 | |
|
|
732 | c.pixel = p; |
|
|
733 | XQueryColor (screen->xdisp, screen->cmap, &c); |
|
|
734 | |
|
|
735 | rgba.r = c.red; |
732 | color.r = c.red; |
736 | rgba.g = c.green; |
733 | color.g = c.green; |
737 | rgba.b = c.blue; |
734 | color.b = c.blue; |
738 | rgba.a = rxvt_rgba::MAX_CC; |
735 | color.a = rgba::MAX_CC; |
739 | #endif |
736 | #endif |
740 | } |
737 | } |
741 | |
738 | |
742 | void |
739 | void |
743 | rxvt_color::free (rxvt_screen *screen) |
740 | rxvt_color::free (rxvt_screen *screen) |
744 | { |
741 | { |
745 | #if XFT |
742 | #if XFT |
746 | XftColorFree (screen->xdisp, screen->visual, screen->cmap, &c); |
743 | XftColorFree (screen->xdisp, screen->visual, screen->cmap, &c); |
747 | #else |
744 | #else |
748 | XFreeColors (screen->xdisp, screen->cmap, &p, 1, AllPlanes); |
745 | XFreeColors (screen->xdisp, screen->cmap, &c.pixel, 1, AllPlanes); |
749 | #endif |
746 | #endif |
750 | } |
747 | } |
751 | |
748 | |
752 | rxvt_color |
749 | void |
753 | rxvt_color::fade (rxvt_screen *screen, int percent) |
750 | rxvt_color::fade (rxvt_screen *screen, int percent, rxvt_color &result, const rgba &to) |
754 | { |
751 | { |
755 | rxvt_color faded; |
|
|
756 | |
|
|
757 | rxvt_rgba c; |
752 | rgba c; |
758 | get (screen, c); |
753 | get (screen, c); |
759 | |
754 | |
760 | c.r = lerp (0, c.r, percent); |
755 | result.set ( |
761 | c.g = lerp (0, c.g, percent); |
|
|
762 | c.b = lerp (0, c.b, percent); |
|
|
763 | |
|
|
764 | faded.set (screen, c); |
|
|
765 | |
|
|
766 | return faded; |
|
|
767 | } |
|
|
768 | |
|
|
769 | rxvt_color |
|
|
770 | rxvt_color::fade (rxvt_screen *screen, int percent, rxvt_color &fadeto) |
|
|
771 | { |
|
|
772 | rxvt_rgba c, fc; |
|
|
773 | rxvt_color faded; |
|
|
774 | |
|
|
775 | get (screen, c); |
|
|
776 | fadeto.get (screen, fc); |
|
|
777 | |
|
|
778 | faded.set ( |
|
|
779 | screen, |
756 | screen, |
780 | rxvt_rgba ( |
757 | rgba ( |
781 | lerp (fc.r, c.r, percent), |
758 | lerp (c.r, to.r, percent), |
782 | lerp (fc.g, c.g, percent), |
759 | lerp (c.g, to.g, percent), |
783 | lerp (fc.b, c.b, percent), |
760 | lerp (c.b, to.b, percent), |
784 | lerp (fc.a, c.a, percent) |
761 | lerp (c.a, to.a, percent) |
785 | ) |
762 | ) |
786 | ); |
763 | ); |
787 | |
|
|
788 | return faded; |
|
|
789 | } |
764 | } |
790 | |
765 | |