aboutsummaryrefslogtreecommitdiff
path: root/st.c
diff options
context:
space:
mode:
authornoname@inventati.org <noname@inventati.org>2015-04-21 09:48:33 +0200
committerRoberto E. Vargas Caballero <k0ga@shike2.com>2015-04-21 09:48:33 +0200
commit89807ed453e27893dea5ac73ee2c6b4f8dd15afb (patch)
tree22db8b7a9010486d2de7257b4b3026f18be4f6f1 /st.c
parent84c756b97e31ca6946f634dc329ff8026caa6539 (diff)
downloadst-89807ed453e27893dea5ac73ee2c6b4f8dd15afb.tar.gz
st-89807ed453e27893dea5ac73ee2c6b4f8dd15afb.zip
Move common code to xloadcolor
Diffstat (limited to 'st.c')
-rw-r--r--st.c82
1 files changed, 29 insertions, 53 deletions
diff --git a/st.c b/st.c
index 7f08748..0204b2e 100644
--- a/st.c
+++ b/st.c
@@ -2898,10 +2898,31 @@ sixd_to_16bit(int x) {
2898 return x == 0 ? 0 : 0x3737 + 0x2828 * x; 2898 return x == 0 ? 0 : 0x3737 + 0x2828 * x;
2899} 2899}
2900 2900
2901bool
2902xloadcolor(int i, const char *name, Color *ncolor) {
2903 XRenderColor color = { .alpha = 0xffff };
2904
2905 if(!name) {
2906 if(BETWEEN(i, 16, 255)) { /* 256 color */
2907 if(i < 6*6*6+16) { /* same colors as xterm */
2908 color.red = sixd_to_16bit( ((i-16)/36)%6 );
2909 color.green = sixd_to_16bit( ((i-16)/6) %6 );
2910 color.blue = sixd_to_16bit( ((i-16)/1) %6 );
2911 } else { /* greyscale */
2912 color.red = 0x0808 + 0x0a0a * (i - (6*6*6+16));
2913 color.green = color.blue = color.red;
2914 }
2915 return XftColorAllocValue(xw.dpy, xw.vis,
2916 xw.cmap, &color, ncolor);
2917 } else
2918 name = colorname[i];
2919 }
2920 return XftColorAllocName(xw.dpy, xw.vis, xw.cmap, name, ncolor);
2921}
2922
2901void 2923void
2902xloadcols(void) { 2924xloadcols(void) {
2903 int i; 2925 int i;
2904 XRenderColor color = { .alpha = 0xffff };
2905 static bool loaded; 2926 static bool loaded;
2906 Color *cp; 2927 Color *cp;
2907 2928
@@ -2910,70 +2931,25 @@ xloadcols(void) {
2910 XftColorFree(xw.dpy, xw.vis, xw.cmap, cp); 2931 XftColorFree(xw.dpy, xw.vis, xw.cmap, cp);
2911 } 2932 }
2912 2933
2913 /* load colors [0-15] and [256-LEN(colorname)] (config.h) */ 2934 for(i = 0; i < LEN(dc.col); i++)
2914 for(i = 0; i < LEN(dc.col); i++) { 2935 if(!xloadcolor(i, NULL, &dc.col[i])) {
2915 if(!colorname[i]) 2936 if(colorname[i])
2916 continue; 2937 die("Could not allocate color '%s'\n", colorname[i]);
2917 if(!XftColorAllocName(xw.dpy, xw.vis, xw.cmap, colorname[i], &dc.col[i])) { 2938 else
2918 die("Could not allocate color '%s'\n", colorname[i]); 2939 die("Could not allocate color %d\n", i);
2919 } 2940 }
2920 }
2921
2922 /* load colors [16-231] ; same colors as xterm */
2923 for(i = 16; i < 6*6*6+16; i++) {
2924 color.red = sixd_to_16bit( ((i-16)/36)%6 );
2925 color.green = sixd_to_16bit( ((i-16)/6) %6 );
2926 color.blue = sixd_to_16bit( ((i-16)/1) %6 );
2927 if(!XftColorAllocValue(xw.dpy, xw.vis, xw.cmap, &color, &dc.col[i]))
2928 die("Could not allocate color %d\n", i);
2929 }
2930
2931 /* load colors [232-255] ; grayscale */
2932 for(; i < 256; i++) {
2933 color.red = color.green = color.blue = 0x0808 + 0x0a0a * (i-(6*6*6+16));
2934 if(!XftColorAllocValue(xw.dpy, xw.vis, xw.cmap, &color, &dc.col[i]))
2935 die("Could not allocate color %d\n", i);
2936 }
2937 loaded = true; 2941 loaded = true;
2938} 2942}
2939 2943
2940int 2944int
2941xsetcolorname(int x, const char *name) { 2945xsetcolorname(int x, const char *name) {
2942 XRenderColor color = { .alpha = 0xffff };
2943 Color ncolor; 2946 Color ncolor;
2944 2947
2945 if(!BETWEEN(x, 0, LEN(dc.col))) 2948 if(!BETWEEN(x, 0, LEN(dc.col)))
2946 return 1; 2949 return 1;
2947 2950
2948 if(!name) {
2949 if(BETWEEN(x, 16, 16 + 215)) { /* 256 color */
2950 color.red = sixd_to_16bit( ((x-16)/36)%6 );
2951 color.green = sixd_to_16bit( ((x-16)/6) %6 );
2952 color.blue = sixd_to_16bit( ((x-16)/1) %6 );
2953 if(!XftColorAllocValue(xw.dpy, xw.vis,
2954 xw.cmap, &color, &ncolor)) {
2955 return 1;
2956 }
2957
2958 XftColorFree(xw.dpy, xw.vis, xw.cmap, &dc.col[x]);
2959 dc.col[x] = ncolor;
2960 return 0;
2961 } else if(BETWEEN(x, 16 + 216, 255)) { /* greyscale */
2962 color.red = color.green = color.blue = \
2963 0x0808 + 0x0a0a * (x - (16 + 216));
2964 if(!XftColorAllocValue(xw.dpy, xw.vis,
2965 xw.cmap, &color, &ncolor)) {
2966 return 1;
2967 }
2968 2951
2969 XftColorFree(xw.dpy, xw.vis, xw.cmap, &dc.col[x]); 2952 if(!xloadcolor(x, name, &ncolor))
2970 dc.col[x] = ncolor;
2971 return 0;
2972 } else { /* system colors */
2973 name = colorname[x];
2974 }
2975 }
2976 if(!XftColorAllocName(xw.dpy, xw.vis, xw.cmap, name, &ncolor))
2977 return 1; 2953 return 1;
2978 2954
2979 XftColorFree(xw.dpy, xw.vis, xw.cmap, &dc.col[x]); 2955 XftColorFree(xw.dpy, xw.vis, xw.cmap, &dc.col[x]);