1 #if !defined HAVE_COLORMIXP_H__
2 #define      HAVE_COLORMIXP_H__
3 // This file is part of the FXT library.
4 // Copyright (C) 2010, 2012 Joerg Arndt
5 // License: GNU General Public License version 3 or later,
6 // see the file COPYING.txt in the main directory.
7 
8 #include "bits/colormix.h"  // color_sum_adjust()
9 #include "fxttypes.h"
10 
11 //<<
12 // Versions of some functionss from colormix.h that
13 // do not ignore the least significant bit
14 // (and are more expensive).
15 //
16 // The non-'perfect' versions should be ok for
17 //   the majority of applications.
18 //>>
19 
perfect_color_mix_50(uint c1,uint c2)20 static inline uint perfect_color_mix_50(uint c1, uint c2)
21 // Return channel-wise average of colors c1 and c2
22 {
23 #if  1
24 //    uint t = (c1 | c2) & 0x010101;  // lowest channels bits in any arg
25     uint t = (c1 & c2) & 0x010101;  // lowest channels bits in both args
26     return  color_mix_50(c1, c2) + t;
27 
28 #else
29     uint m = 0xff00ff;
30     uint srb = (c1 & m) + (c2 & m) + 0x010001;
31     srb >>= 1;
32     srb &= m;
33 
34     m >>= 8;  // == 0xff00
35     uint sg = (c1 & m) + (c2 & m) + 0x0100;
36     sg >>= 1;
37     sg &= m;
38 
39     return  srb | sg;
40 #endif
41 }
42 // -------------------------
43 
perfect_color_mix_75(uint c1,uint c2)44 static inline uint perfect_color_mix_75(uint c1, uint c2)
45 {
46     return  perfect_color_mix_50(c1, perfect_color_mix_50(c1, c2));  // 75% c1
47 }
48 // -------------------------
49 
perfect_color_sum(uint c1,uint c2)50 static inline uint perfect_color_sum(uint c1, uint c2)
51 {
52     uint srb = (c1 & 0xff00ff) + (c2 & 0xff00ff) + 0x010001;
53     uint mrb = srb & 0x01000100;
54     srb ^= mrb;
55     uint sg = (c1 & 0xff00) + (c2 & 0xff00) + 0x0100;
56     uint mg = (sg & 0x010000);
57     sg ^= mg;
58     uint m = (mrb | mg) >> 1;  // 1000 0000  // overflow bits
59     m |= (m >> 1);  // 1100 0000
60     m |= (m >> 2);  // 1111 0000
61     m |= (m >> 4);  // 1111 1111
62     return  srb | sg | m;
63 }
64 // -------------------------
65 
perfect_color_sum(uint c0,uint c1,uint c2)66 static inline uint perfect_color_sum(uint c0, uint c1, uint c2)
67 {
68     return  perfect_color_sum( perfect_color_sum(c1, c2), c0 );
69 }
70 // -------------------------
71 
72 
73 #endif  // !defined HAVE_COLORMIXP_H__
74