… | |
… | |
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 rxvt_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 / rxvt_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 / rxvt_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 / rxvt_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 / rxvt_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 | p = (rgba.r * (screen->visual->red_mask >> ctz (screen->visual->red_mask )) |
603 | p = (color.r * (screen->visual->red_mask >> ctz (screen->visual->red_mask )) |
604 | / rxvt_rgba::MAX_CC) << ctz (screen->visual->red_mask ) |
604 | / rxvt_rgba::MAX_CC) << ctz (screen->visual->red_mask ) |
605 | | (rgba.g * (screen->visual->green_mask >> ctz (screen->visual->green_mask)) |
605 | | (color.g * (screen->visual->green_mask >> ctz (screen->visual->green_mask)) |
606 | / rxvt_rgba::MAX_CC) << ctz (screen->visual->green_mask) |
606 | / rxvt_rgba::MAX_CC) << ctz (screen->visual->green_mask) |
607 | | (rgba.b * (screen->visual->blue_mask >> ctz (screen->visual->blue_mask )) |
607 | | (color.b * (screen->visual->blue_mask >> ctz (screen->visual->blue_mask )) |
608 | / rxvt_rgba::MAX_CC) << ctz (screen->visual->blue_mask ); |
608 | / rxvt_rgba::MAX_CC) << ctz (screen->visual->blue_mask ); |
609 | |
609 | |
610 | return true; |
610 | return true; |
611 | } |
611 | } |
612 | else |
612 | else |
613 | { |
613 | { |
614 | XColor xc; |
614 | XColor xc; |
615 | |
615 | |
616 | xc.red = rgba.r; |
616 | xc.red = color.r; |
617 | xc.green = rgba.g; |
617 | xc.green = color.g; |
618 | xc.blue = rgba.b; |
618 | xc.blue = color.b; |
619 | |
619 | |
620 | if (XAllocColor (screen->xdisp, screen->cmap, &xc)) |
620 | if (XAllocColor (screen->xdisp, screen->cmap, &xc)) |
621 | { |
621 | { |
622 | p = xc.pixel; |
622 | p = xc.pixel; |
623 | return true; |
623 | return true; |
624 | } |
624 | } |
625 | else |
625 | else |
626 | p = (rgba.r + rgba.g + rgba.b) > 128*3 |
626 | p = (color.r + color.g + color.b) > 128*3 |
627 | ? WhitePixelOfScreen (DefaultScreenOfDisplay (screen->xdisp)) |
627 | ? WhitePixelOfScreen (DefaultScreenOfDisplay (screen->xdisp)) |
628 | : BlackPixelOfScreen (DefaultScreenOfDisplay (screen->xdisp)); |
628 | : BlackPixelOfScreen (DefaultScreenOfDisplay (screen->xdisp)); |
629 | } |
629 | } |
630 | #endif |
630 | #endif |
631 | |
631 | |
… | |
… | |
677 | |
677 | |
678 | return set (screen, c); |
678 | return set (screen, c); |
679 | } |
679 | } |
680 | |
680 | |
681 | bool |
681 | bool |
682 | rxvt_color::set (rxvt_screen *screen, rxvt_rgba rgba) |
682 | rxvt_color::set (rxvt_screen *screen, const rxvt_rgba &color) |
683 | { |
683 | { |
684 | bool got = alloc (screen, rgba); |
684 | bool got = alloc (screen, color); |
685 | |
685 | |
686 | #if !ENABLE_MINIMAL |
686 | #if !ENABLE_MINIMAL |
687 | int cmap_size = screen->visual->map_entries; |
687 | int cmap_size = screen->visual->map_entries; |
688 | |
688 | |
689 | if (!got |
689 | if (!got |
… | |
… | |
700 | int diff = 0x7fffffffUL; |
700 | int diff = 0x7fffffffUL; |
701 | XColor *best = colors; |
701 | XColor *best = colors; |
702 | |
702 | |
703 | for (int i = 0; i < cmap_size; i++) |
703 | for (int i = 0; i < cmap_size; i++) |
704 | { |
704 | { |
705 | int d = (squared_diff<int> (rgba.r >> 2, colors [i].red >> 2)) |
705 | int d = (squared_diff<int> (color.r >> 2, colors [i].red >> 2)) |
706 | + (squared_diff<int> (rgba.g >> 2, colors [i].green >> 2)) |
706 | + (squared_diff<int> (color.g >> 2, colors [i].green >> 2)) |
707 | + (squared_diff<int> (rgba.b >> 2, colors [i].blue >> 2)); |
707 | + (squared_diff<int> (color.b >> 2, colors [i].blue >> 2)); |
708 | |
708 | |
709 | if (d < diff) |
709 | if (d < diff) |
710 | { |
710 | { |
711 | diff = d; |
711 | diff = d; |
712 | best = colors + i; |
712 | best = colors + i; |
713 | } |
713 | } |
714 | } |
714 | } |
715 | |
715 | |
716 | //rxvt_warn ("could not allocate %04x %04x %04x, getting %04x %04x %04x instead (%d)\n", |
716 | //rxvt_warn ("could not allocate %04x %04x %04x, getting %04x %04x %04x instead (%d)\n", |
717 | // rgba.r, rgba.g, rgba.b, best->red, best->green, best->blue, diff); |
717 | // color.r, color.g, color.b, best->red, best->green, best->blue, diff); |
718 | |
718 | |
719 | got = alloc (screen, rxvt_rgba (best->red, best->green, best->blue)); |
719 | got = alloc (screen, rxvt_rgba (best->red, best->green, best->blue)); |
720 | |
720 | |
721 | delete colors; |
721 | delete colors; |
722 | } |
722 | } |
… | |
… | |
724 | |
724 | |
725 | return got; |
725 | return got; |
726 | } |
726 | } |
727 | |
727 | |
728 | void |
728 | void |
729 | rxvt_color::get (rxvt_screen *screen, rxvt_rgba &rgba) |
729 | rxvt_color::get (rxvt_screen *screen, rxvt_rgba &color) |
730 | { |
730 | { |
731 | #if XFT |
731 | #if XFT |
732 | rgba.r = c.color.red; |
732 | color.r = c.color.red; |
733 | rgba.g = c.color.green; |
733 | color.g = c.color.green; |
734 | rgba.b = c.color.blue; |
734 | color.b = c.color.blue; |
735 | rgba.a = c.color.alpha; |
735 | color.a = c.color.alpha; |
736 | #else |
736 | #else |
737 | XColor c; |
737 | XColor c; |
738 | |
738 | |
739 | c.pixel = p; |
739 | c.pixel = p; |
740 | XQueryColor (screen->xdisp, screen->cmap, &c); |
740 | XQueryColor (screen->xdisp, screen->cmap, &c); |
741 | |
741 | |
742 | rgba.r = c.red; |
742 | color.r = c.red; |
743 | rgba.g = c.green; |
743 | color.g = c.green; |
744 | rgba.b = c.blue; |
744 | color.b = c.blue; |
745 | rgba.a = rxvt_rgba::MAX_CC; |
745 | color.a = rxvt_rgba::MAX_CC; |
746 | #endif |
746 | #endif |
747 | } |
747 | } |
748 | |
748 | |
749 | void |
749 | void |
750 | rxvt_color::free (rxvt_screen *screen) |
750 | rxvt_color::free (rxvt_screen *screen) |
… | |
… | |
755 | XFreeColors (screen->xdisp, screen->cmap, &p, 1, AllPlanes); |
755 | XFreeColors (screen->xdisp, screen->cmap, &p, 1, AllPlanes); |
756 | #endif |
756 | #endif |
757 | } |
757 | } |
758 | |
758 | |
759 | rxvt_color |
759 | rxvt_color |
760 | rxvt_color::fade (rxvt_screen *screen, int percent) |
760 | rxvt_color::fade (rxvt_screen *screen, int percent, const rxvt_rgba &to) |
761 | { |
761 | { |
762 | rxvt_color faded; |
|
|
763 | |
|
|
764 | rxvt_rgba c; |
762 | rxvt_rgba c; |
765 | get (screen, c); |
763 | get (screen, c); |
766 | |
764 | |
767 | c.r = lerp (0, c.r, percent); |
|
|
768 | c.g = lerp (0, c.g, percent); |
|
|
769 | c.b = lerp (0, c.b, percent); |
|
|
770 | |
|
|
771 | faded.set (screen, c); |
|
|
772 | |
|
|
773 | return faded; |
|
|
774 | } |
|
|
775 | |
|
|
776 | rxvt_color |
|
|
777 | rxvt_color::fade (rxvt_screen *screen, int percent, rxvt_color &fadeto) |
|
|
778 | { |
|
|
779 | rxvt_rgba c, fc; |
|
|
780 | rxvt_color faded; |
765 | rxvt_color faded; |
781 | |
|
|
782 | get (screen, c); |
|
|
783 | fadeto.get (screen, fc); |
|
|
784 | |
|
|
785 | faded.set ( |
766 | faded.set ( |
786 | screen, |
767 | screen, |
787 | rxvt_rgba ( |
768 | rxvt_rgba ( |
788 | lerp (fc.r, c.r, percent), |
769 | lerp (to.r, c.r, percent), |
789 | lerp (fc.g, c.g, percent), |
770 | lerp (to.g, c.g, percent), |
790 | lerp (fc.b, c.b, percent), |
771 | lerp (to.b, c.b, percent), |
791 | lerp (fc.a, c.a, percent) |
772 | lerp (to.a, c.a, percent) |
792 | ) |
773 | ) |
793 | ); |
774 | ); |
794 | |
775 | |
795 | return faded; |
776 | return faded; |
796 | } |
777 | } |