Ticket #3145: mc-truecolor-demo.patch

File mc-truecolor-demo.patch, 2.3 KB (added by egmont, 10 years ago)
  • lib/tty/color-internal.c

    diff -ur mc-4.8.11.orig/lib/tty/color-internal.c mc-4.8.11/lib/tty/color-internal.c
    old new  
    9494/*** file scope functions ************************************************************************/ 
    9595/* --------------------------------------------------------------------------------------------- */ 
    9696 
     97static inline int 
     98parse_hex_digit (char c) 
     99{ 
     100   if (c >= '0' && c <= '9') 
     101       return c - '0'; 
     102   c |= 0x20; 
     103   if (c >= 'a' && c <= 'f') 
     104       return c - 'a' + 10; 
     105   return -1; 
     106} 
     107 
    97108static int 
    98 parse_256_color_name (const char *color_name) 
     109parse_256_or_true_color_name (const char *color_name) 
    99110{ 
    100111    int i; 
    101112    char dummy; 
     
    117128    { 
    118129        return 16 + 36 * (color_name[3] - '0') + 6 * (color_name[4] - '0') + (color_name[5] - '0'); 
    119130    } 
     131    if (color_name[0] == '#') 
     132    { 
     133        int h[6]; 
     134        color_name++;  
     135        if (strlen(color_name) != 3 && strlen(color_name) != 6) 
     136            return -1; 
     137        for (i = 0; color_name[i] != '\0'; i++) { 
     138            h[i] = parse_hex_digit (color_name[i]); 
     139            if (h[i] == -1) 
     140                return -1;    
     141        } 
     142        if (i == 3) 
     143            i = (h[0] << 20) | (h[0] << 16) | (h[1] << 12) | (h[1] << 8) | (h[2] << 4) | h[2]; 
     144        else 
     145            i = (h[0] << 20) | (h[1] << 16) | (h[2] << 12) | (h[3] << 8) | (h[4] << 4) | h[5]; 
     146        return (1 << 24) | i; 
     147    } 
    120148    return -1; 
    121149} 
    122150 
     
    150178        } 
    151179        return color_N_names[idx - 16]; 
    152180    } 
     181    /* Create and return strings in "#rrggbb" format for true color. */ 
     182    if (idx & (1 << 24)) 
     183    { 
     184        // FIXME: The allocated string is not free()d. 
     185        // Not trivial to fix, needs some refactoring. 
     186        gchar *rgb = g_try_malloc(8); 
     187        sprintf(rgb, "#%06X", idx & 0xFFFFFF); 
     188        return rgb; 
     189    } 
    153190    return "default"; 
    154191} 
    155192 
     
    165202        for (i = 0; color_table[i].name != NULL; i++) 
    166203            if (strcmp (color_name, color_table[i].name) == 0) 
    167204                return color_table[i].value; 
    168         return parse_256_color_name (color_name); 
     205        return parse_256_or_true_color_name (color_name); 
    169206    } 
    170207    return -1; 
    171208}