Changeset 1266


Ignore:
Timestamp:
Oct 31, 2006, 9:17:58 AM (16 years ago)
Author:
Sam Hocevar
Message:
  • Changed cucul_set_attr() so that only setting the style does not change the colour.
Location:
libcaca/trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • libcaca/trunk/cucul/colour.c

    r1258 r1266  
    3838 *  or created using cucul_ansi_to_attr() and cucul_argb_to_attr(), optionally
    3939 *  ORed with style values such as CUCUL_UNDERLINE or CUCUL_BLINK.
     40 *
     41 *  Only changing the style does not affect the current colour value.
    4042 *
    4143 *  If an error occurs, -1 is returned and \b errno is set accordingly:
     
    5658    }
    5759
     60    if(attr < 0x00000010)
     61        attr = (cv->curattr & 0xfffffff0) | attr;
     62
    5863    cv->curattr = attr;
    5964
    6065    return 0;
    61 }
    62 
    63 /** \brief Set the default colour pair and text style (ANSI version).
    64  *
    65  *  Set the default ANSI colour pair and text style for drawing. String
    66  *  functions such as caca_printf() and graphical primitive functions such as
    67  *  caca_draw_line() will use these attributes.
    68  *
    69  *  Color values are those defined in cucul.h, such as CUCUL_RED
    70  *  or CUCUL_TRANSPARENT.
    71  *
    72  *  Style values are those defined in cucul.h, such as CUCUL_UNDERLINE
    73  *  or CUCUL_BLINK. The values can be ORed to set several styles at
    74  *  the same time.
    75  *
    76  *  If an error occurs, 0 is returned and \b errno is set accordingly:
    77  *  - \c EINVAL The colour values and/or the style mask are invalid.
    78  *
    79  *  \param cv A handle to the libcucul canvas.
    80  *  \param fg The requested foreground colour.
    81  *  \param bg The requested background colour.
    82  *  \param style The requested text styles.
    83  *  \return 0 in case of success, -1 if an error occurred.
    84  */
    85 unsigned long int cucul_ansi_to_attr(unsigned char fg, unsigned char bg)
    86 {
    87     if(fg > 0x20 || bg > 0x20)
    88     {
    89 #if defined(HAVE_ERRNO_H)
    90         errno = EINVAL;
    91 #endif
    92         return 0;
    93     }
    94 
    95     return ((unsigned long int)bg << 18) | ((unsigned long int)fg << 4);
    96 }
    97 
    98 /* Legacy function for old programs */
    99 int cucul_set_color(cucul_canvas_t *cv, unsigned char fg, unsigned char bg)
    100 {
    101     return cucul_set_attr(cv, cucul_ansi_to_attr(fg, bg));
    102 }
    103 
    104 /** \brief Set the default colour pair and text style (truecolor version).
    105  *
    106  *  Set the default colour pair and text style for drawing. String
    107  *  functions such as caca_printf() and graphical primitive functions such as
    108  *  caca_draw_line() will use these attributes.
    109  *
    110  *  Colors are 16-bit ARGB values, each component being coded on 4 bits. For
    111  *  instance, 0xf088 is solid dark cyan (A=15 R=0 G=8 B=8), and 0x8fff is
    112  *  white with 50% alpha (A=8 R=15 G=15 B=15).
    113  *
    114  *  If an error occurs, 0 is returned and \b errno is set accordingly:
    115  *  - \c EINVAL At least one of the colour values is invalid.
    116  *
    117  *  \param cv A handle to the libcucul canvas.
    118  *  \param fg The requested foreground colour.
    119  *  \param bg The requested background colour.
    120  *  \param style The requested text styles.
    121  *  \return 0 in case of success, -1 if an error occurred.
    122  */
    123 unsigned long int cucul_argb_to_attr(unsigned int fg, unsigned int bg)
    124 {
    125     if(fg > 0xffff || bg > 0xffff)
    126     {
    127 #if defined(HAVE_ERRNO_H)
    128         errno = EINVAL;
    129 #endif
    130         return -1;
    131     }
    132 
    133     if(fg < 0x100)
    134         fg += 0x100;
    135 
    136     if(bg < 0x100)
    137         bg += 0x100;
    138 
    139     fg = ((fg >> 1) & 0x7ff) | ((fg >> 13) << 11);
    140     bg = ((bg >> 1) & 0x7ff) | ((bg >> 13) << 11);
    141 
    142     return ((unsigned long int)bg << 18) | ((unsigned long int)fg << 4);
    143 }
    144 
    145 /* Legacy function for old programs */
    146 int cucul_set_truecolor(cucul_canvas_t *cv, unsigned int fg, unsigned int bg)
    147 {
    148     return cucul_set_attr(cv, cucul_argb_to_attr(fg, bg));
    14966}
    15067
     
    18299}
    183100
     101/** \brief Set the default colour pair and text style (ANSI version).
     102 *
     103 *  Set the default ANSI colour pair and text style for drawing. String
     104 *  functions such as caca_printf() and graphical primitive functions such as
     105 *  caca_draw_line() will use these attributes.
     106 *
     107 *  Color values are those defined in cucul.h, such as CUCUL_RED
     108 *  or CUCUL_TRANSPARENT.
     109 *
     110 *  Style values are those defined in cucul.h, such as CUCUL_UNDERLINE
     111 *  or CUCUL_BLINK. The values can be ORed to set several styles at
     112 *  the same time.
     113 *
     114 *  If an error occurs, 0 is returned and \b errno is set accordingly:
     115 *  - \c EINVAL The colour values and/or the style mask are invalid.
     116 *
     117 *  \param cv A handle to the libcucul canvas.
     118 *  \param fg The requested foreground colour.
     119 *  \param bg The requested background colour.
     120 *  \param style The requested text styles.
     121 *  \return 0 in case of success, -1 if an error occurred.
     122 */
     123unsigned long int cucul_ansi_to_attr(unsigned char fg, unsigned char bg)
     124{
     125    if(fg > 0x20 || bg > 0x20)
     126    {
     127#if defined(HAVE_ERRNO_H)
     128        errno = EINVAL;
     129#endif
     130        return 0;
     131    }
     132
     133    fg |= 0x40;
     134    bg |= 0x40;
     135
     136    return ((unsigned long int)bg << 18) | ((unsigned long int)fg << 4);
     137}
     138
     139/* Legacy function for old programs */
     140int cucul_set_color(cucul_canvas_t *cv, unsigned char fg, unsigned char bg)
     141{
     142    return cucul_set_attr(cv, cucul_ansi_to_attr(fg, bg));
     143}
     144
     145/** \brief Set the default colour pair and text style (truecolor version).
     146 *
     147 *  Set the default colour pair and text style for drawing. String
     148 *  functions such as caca_printf() and graphical primitive functions such as
     149 *  caca_draw_line() will use these attributes.
     150 *
     151 *  Colors are 16-bit ARGB values, each component being coded on 4 bits. For
     152 *  instance, 0xf088 is solid dark cyan (A=15 R=0 G=8 B=8), and 0x8fff is
     153 *  white with 50% alpha (A=8 R=15 G=15 B=15).
     154 *
     155 *  If an error occurs, 0 is returned and \b errno is set accordingly:
     156 *  - \c EINVAL At least one of the colour values is invalid.
     157 *
     158 *  \param cv A handle to the libcucul canvas.
     159 *  \param fg The requested foreground colour.
     160 *  \param bg The requested background colour.
     161 *  \param style The requested text styles.
     162 *  \return 0 in case of success, -1 if an error occurred.
     163 */
     164unsigned long int cucul_argb_to_attr(unsigned int fg, unsigned int bg)
     165{
     166    if(fg > 0xffff || bg > 0xffff)
     167    {
     168#if defined(HAVE_ERRNO_H)
     169        errno = EINVAL;
     170#endif
     171        return -1;
     172    }
     173
     174    if(fg < 0x100)
     175        fg += 0x100;
     176
     177    if(bg < 0x100)
     178        bg += 0x100;
     179
     180    fg = ((fg >> 1) & 0x7ff) | ((fg >> 13) << 11);
     181    bg = ((bg >> 1) & 0x7ff) | ((bg >> 13) << 11);
     182
     183    return ((unsigned long int)bg << 18) | ((unsigned long int)fg << 4);
     184}
     185
     186/* Legacy function for old programs */
     187int cucul_set_truecolor(cucul_canvas_t *cv, unsigned int fg, unsigned int bg)
     188{
     189    return cucul_set_attr(cv, cucul_argb_to_attr(fg, bg));
     190}
     191
    184192/*
    185193 * XXX: the following functions are local
     
    206214    unsigned int i, best, dist;
    207215
    208     if(argb14 < 0x0010)
    209         return argb14;
    210 
    211     if(argb14 == CUCUL_DEFAULT || argb14 == CUCUL_TRANSPARENT)
     216    if(argb14 < 0x0050)
     217        return argb14 ^ 0x40;
     218
     219    if(argb14 == (CUCUL_DEFAULT | 0x40) || argb14 == (CUCUL_TRANSPARENT | 0x40))
    212220        return def;
    213221
     
    267275    uint16_t fg = (attr >> 4) & 0x3fff;
    268276
    269     if(fg < 0x0010)
    270         return ansitab16[fg] & 0x0fff;
    271 
    272     if(fg == CUCUL_DEFAULT)
     277    if(fg < 0x0050)
     278        return ansitab16[fg ^ 0x40] & 0x0fff;
     279
     280    if(fg == (CUCUL_DEFAULT | 0x40))
    273281        return ansitab16[CUCUL_LIGHTGRAY] & 0x0fff;
    274282
    275     if(fg == CUCUL_TRANSPARENT)
     283    if(fg == (CUCUL_TRANSPARENT | 0x40))
    276284        return ansitab16[CUCUL_LIGHTGRAY] & 0x0fff;
    277285
     
    283291    uint16_t bg = attr >> 18;
    284292
    285     if(bg < 0x0010)
    286         return ansitab16[bg] & 0x0fff;
    287 
    288     if(bg == CUCUL_DEFAULT)
     293    if(bg < 0x0050)
     294        return ansitab16[bg ^ 0x40] & 0x0fff;
     295
     296    if(bg == (CUCUL_DEFAULT | 0x40))
    289297        return ansitab16[CUCUL_BLACK] & 0x0fff;
    290298
    291     if(bg == CUCUL_TRANSPARENT)
     299    if(bg == (CUCUL_TRANSPARENT | 0x40))
    292300        return ansitab16[CUCUL_BLACK] & 0x0fff;
    293301
     
    315323    uint16_t bg = attr >> 18;
    316324
    317     if(bg < 0x0010)
    318         bg = ansitab16[bg];
    319     else if(bg == CUCUL_DEFAULT)
     325    if(bg < 0x0050)
     326        bg = ansitab16[bg ^ 0x40];
     327    else if(bg == (CUCUL_DEFAULT | 0x40))
    320328        bg = ansitab16[CUCUL_BLACK];
    321     else if(bg == CUCUL_TRANSPARENT)
     329    else if(bg == (CUCUL_TRANSPARENT | 0x40))
    322330        bg = 0x0fff;
    323331    else
     
    329337    argb[3] = bg & 0xf;
    330338
    331     if(fg < 0x0010)
    332         fg = ansitab16[fg];
    333     else if(fg == CUCUL_DEFAULT)
     339    if(fg < 0x0050)
     340        fg = ansitab16[fg ^ 0x40];
     341    else if(fg == (CUCUL_DEFAULT | 0x40))
    334342        fg = ansitab16[CUCUL_LIGHTGRAY];
    335     else if(fg == CUCUL_TRANSPARENT)
     343    else if(fg == (CUCUL_TRANSPARENT | 0x40))
    336344        fg = 0x0fff;
    337345    else
  • libcaca/trunk/cucul/export.c

    r1264 r1266  
    231231                continue;
    232232
    233             fg = (((attr >> 4) & 0x3fff) == CUCUL_DEFAULT) ?
     233            fg = (((attr >> 4) & 0x3fff) == (CUCUL_DEFAULT | 0x40)) ?
    234234                     0x10 : palette[_cucul_attr_to_ansi4fg(attr)];
    235             bg = (((attr >> 18) & 0x3fff) == CUCUL_TRANSPARENT) ?
     235            bg = (((attr >> 18) & 0x3fff) == (CUCUL_TRANSPARENT | 0x40)) ?
    236236                     0x10 : palette[_cucul_attr_to_ansi4bg(attr)];
    237237
     
    567567                continue;
    568568
    569             if(((attr >> 4) & 0x3fff) == CUCUL_DEFAULT)
     569            if(((attr >> 4) & 0x3fff) == (CUCUL_DEFAULT | 0x40))
    570570                fg = 0x10;
    571571
    572             if(((attr >> 18) & 0x3fff) == CUCUL_TRANSPARENT)
     572            if(((attr >> 18) & 0x3fff) == (CUCUL_TRANSPARENT | 0x40))
    573573                bg = 0x10;
    574574
  • libcaca/trunk/test/colors.c

    r1263 r1266  
    5353    cucul_set_attr(cv, cucul_ansi_to_attr(CUCUL_LIGHTGRAY, CUCUL_BLACK));
    5454    cucul_putstr(cv, 3, 20, "This is bold    This is blink    This is italics    This is underline");
    55     cucul_set_attr(cv, cucul_ansi_to_attr(CUCUL_LIGHTGRAY, CUCUL_BLACK) | CUCUL_BOLD);
     55    cucul_set_attr(cv, CUCUL_BOLD);
    5656    cucul_putstr(cv, 3 + 8, 20, "bold");
    57     cucul_set_attr(cv, cucul_ansi_to_attr(CUCUL_LIGHTGRAY, CUCUL_BLACK) | CUCUL_BLINK);
     57    cucul_set_attr(cv, CUCUL_BLINK);
    5858    cucul_putstr(cv, 3 + 24, 20, "blink");
    59     cucul_set_attr(cv, cucul_ansi_to_attr(CUCUL_LIGHTGRAY, CUCUL_BLACK) | CUCUL_ITALICS);
     59    cucul_set_attr(cv, CUCUL_ITALICS);
    6060    cucul_putstr(cv, 3 + 41, 20, "italics");
    61     cucul_set_attr(cv, cucul_ansi_to_attr(CUCUL_LIGHTGRAY, CUCUL_BLACK) | CUCUL_UNDERLINE);
     61    cucul_set_attr(cv, CUCUL_UNDERLINE);
    6262    cucul_putstr(cv, 3 + 60, 20, "underline");
    6363
  • libcaca/trunk/test/export.c

    r1263 r1266  
    127127        cucul_putstr(cv, WIDTH / 2 - 7, HEIGHT / 2 + 2, "äβç ░▒▓█▓▒░ ΔЗҒ");
    128128
    129         cucul_set_attr(cv, cucul_ansi_to_attr(CUCUL_BLACK, CUCUL_WHITE)
    130                             | CUCUL_BOLD);
     129        cucul_set_attr(cv, CUCUL_BOLD);
    131130        cucul_putstr(cv, WIDTH / 2 - 16, HEIGHT / 2 + 3, "Bold");
    132         cucul_set_attr(cv, cucul_ansi_to_attr(CUCUL_BLACK, CUCUL_WHITE)
    133                             | CUCUL_BLINK);
     131        cucul_set_attr(cv, CUCUL_BLINK);
    134132        cucul_putstr(cv, WIDTH / 2 - 9, HEIGHT / 2 + 3, "Blink");
    135         cucul_set_attr(cv, cucul_ansi_to_attr(CUCUL_BLACK, CUCUL_WHITE)
    136                             | CUCUL_ITALICS);
     133        cucul_set_attr(cv, CUCUL_ITALICS);
    137134        cucul_putstr(cv, WIDTH / 2 - 1, HEIGHT / 2 + 3, "Italics");
    138         cucul_set_attr(cv, cucul_ansi_to_attr(CUCUL_BLACK, CUCUL_WHITE)
    139                             | CUCUL_UNDERLINE);
     135        cucul_set_attr(cv, CUCUL_UNDERLINE);
    140136        cucul_putstr(cv, WIDTH / 2 + 8, HEIGHT / 2 + 3, "Underline");
    141137
Note: See TracChangeset for help on using the changeset viewer.