Changeset 2821 for libcaca/trunk/caca/attr.c
- Timestamp:
- Sep 27, 2008, 3:12:46 PM (12 years ago)
- File:
-
- 1 copied
Legend:
- Unmodified
- Added
- Removed
-
libcaca/trunk/caca/attr.c
r2819 r2821 1 1 /* 2 * libc ucul Canvas for ultrafast compositing of Unicode letters2 * libcaca Colour ASCII-Art library 3 3 * Copyright (c) 2002-2006 Sam Hocevar <sam@zoy.org> 4 4 * All Rights Reserved … … 20 20 #include "config.h" 21 21 22 #include "c ucul.h"23 #include "c ucul_internals.h"22 #include "caca.h" 23 #include "caca_internals.h" 24 24 25 25 static uint8_t nearest_ansi(uint16_t); … … 43 43 /** \brief Get the text attribute at the given coordinates. 44 44 * 45 * Get the internal \e libc uculattribute value of the character at the45 * Get the internal \e libcaca attribute value of the character at the 46 46 * given coordinates. The attribute value has 32 significant bits, 47 47 * organised as follows from MSB to LSB: … … 61 61 * This function never fails. 62 62 * 63 * \param cv A handle to the libc uculcanvas.63 * \param cv A handle to the libcaca canvas. 64 64 * \param x X coordinate. 65 65 * \param y Y coordinate. 66 66 * \return The requested attribute. 67 67 */ 68 uint32_t c ucul_get_attr(cucul_canvas_t const *cv, int x, int y)68 uint32_t caca_get_attr(caca_canvas_t const *cv, int x, int y) 69 69 { 70 70 if(x < 0 || x >= (int)cv->width || y < 0 || y >= (int)cv->height) … … 83 83 * 84 84 * The value of \e attr is either: 85 * - a 32-bit integer as returned by c ucul_get_attr(), in which case it85 * - a 32-bit integer as returned by caca_get_attr(), in which case it 86 86 * also contains colour information, 87 * - a combination (bitwise OR) of style values (\e C UCUL_UNDERLINE,88 * \e C UCUL_BLINK, \e CUCUL_BOLD and \e CUCUL_ITALICS), in which case87 * - a combination (bitwise OR) of style values (\e CACA_UNDERLINE, 88 * \e CACA_BLINK, \e CACA_BOLD and \e CACA_ITALICS), in which case 89 89 * setting the attribute does not modify the current colour information. 90 90 * 91 * To retrieve the current attribute value, use c ucul_get_attr(-1,-1).91 * To retrieve the current attribute value, use caca_get_attr(-1,-1). 92 92 * 93 93 * This function never fails. 94 94 * 95 * \param cv A handle to the libc uculcanvas.95 * \param cv A handle to the libcaca canvas. 96 96 * \param attr The requested attribute value. 97 97 * \return This function always returns 0. 98 98 */ 99 int c ucul_set_attr(cucul_canvas_t *cv, uint32_t attr)99 int caca_set_attr(caca_canvas_t *cv, uint32_t attr) 100 100 { 101 101 if(attr < 0x00000010) … … 114 114 * 115 115 * The value of \e attr is either: 116 * - a 32-bit integer as returned by c ucul_get_attr(), in which case it116 * - a 32-bit integer as returned by caca_get_attr(), in which case it 117 117 * also contains colour information, 118 * - a combination (bitwise OR) of style values (\e C UCUL_UNDERLINE,119 * \e C UCUL_BLINK, \e CUCUL_BOLD and \e CUCUL_ITALICS), in which case118 * - a combination (bitwise OR) of style values (\e CACA_UNDERLINE, 119 * \e CACA_BLINK, \e CACA_BOLD and \e CACA_ITALICS), in which case 120 120 * setting the attribute does not modify the current colour information. 121 121 * 122 122 * This function never fails. 123 123 * 124 * \param cv A handle to the libc uculcanvas.124 * \param cv A handle to the libcaca canvas. 125 125 * \param x X coordinate. 126 126 * \param y Y coordinate. … … 128 128 * \return This function always returns 0. 129 129 */ 130 int c ucul_put_attr(cucul_canvas_t *cv, int x, int y, uint32_t attr)130 int caca_put_attr(caca_canvas_t *cv, int x, int y, uint32_t attr) 131 131 { 132 132 uint32_t *curattr, *curchar; … … 143 143 curattr[0] = attr; 144 144 145 if(x && curchar[0] == C UCUL_MAGIC_FULLWIDTH)145 if(x && curchar[0] == CACA_MAGIC_FULLWIDTH) 146 146 curattr[-1] = curattr[0]; 147 else if(x + 1 < (int)cv->width && curchar[1] == C UCUL_MAGIC_FULLWIDTH)147 else if(x + 1 < (int)cv->width && curchar[1] == CACA_MAGIC_FULLWIDTH) 148 148 curattr[1] = curattr[0]; 149 149 … … 157 157 * will use these attributes. 158 158 * 159 * Color values are those defined in c ucul.h, such as CUCUL_RED160 * or C UCUL_TRANSPARENT.159 * Color values are those defined in caca.h, such as CACA_RED 160 * or CACA_TRANSPARENT. 161 161 * 162 162 * If an error occurs, 0 is returned and \b errno is set accordingly: 163 163 * - \c EINVAL At least one of the colour values is invalid. 164 164 * 165 * \param cv A handle to the libc uculcanvas.165 * \param cv A handle to the libcaca canvas. 166 166 * \param fg The requested ANSI foreground colour. 167 167 * \param bg The requested ANSI background colour. 168 168 * \return 0 in case of success, -1 if an error occurred. 169 169 */ 170 int c ucul_set_color_ansi(cucul_canvas_t *cv, uint8_t fg, uint8_t bg)170 int caca_set_color_ansi(caca_canvas_t *cv, uint8_t fg, uint8_t bg) 171 171 { 172 172 uint32_t attr; … … 196 196 * This function never fails. 197 197 * 198 * \param cv A handle to the libc uculcanvas.198 * \param cv A handle to the libcaca canvas. 199 199 * \param fg The requested ARGB foreground colour. 200 200 * \param bg The requested ARGB background colour. 201 201 * \return This function always returns 0. 202 202 */ 203 int c ucul_set_color_argb(cucul_canvas_t *cv, uint16_t fg, uint16_t bg)203 int caca_set_color_argb(caca_canvas_t *cv, uint16_t fg, uint16_t bg) 204 204 { 205 205 uint32_t attr; … … 227 227 * 228 228 * If the attribute has ARGB colours, the nearest colour is used. Special 229 * attributes such as \e C UCUL_DEFAULT and \e CUCUL_TRANSPARENT are not230 * handled and are both replaced with \e C UCUL_LIGHTGRAY for the foreground231 * colour and \e C UCUL_BLACK for the background colour.229 * attributes such as \e CACA_DEFAULT and \e CACA_TRANSPARENT are not 230 * handled and are both replaced with \e CACA_LIGHTGRAY for the foreground 231 * colour and \e CACA_BLACK for the background colour. 232 232 * 233 233 * This function never fails. If the attribute value is outside the expected … … 237 237 * \return The corresponding DOS ANSI value. 238 238 */ 239 uint8_t c ucul_attr_to_ansi(uint32_t attr)239 uint8_t caca_attr_to_ansi(uint32_t attr) 240 240 { 241 241 uint8_t fg = nearest_ansi((attr >> 4) & 0x3fff); 242 242 uint8_t bg = nearest_ansi(attr >> 18); 243 243 244 return (fg < 0x10 ? fg : C UCUL_LIGHTGRAY)245 | ((bg < 0x10 ? bg : C UCUL_BLACK) << 4);244 return (fg < 0x10 ? fg : CACA_LIGHTGRAY) 245 | ((bg < 0x10 ? bg : CACA_BLACK) << 4); 246 246 } 247 247 … … 249 249 * 250 250 * Get the ANSI foreground colour value for a given attribute. The returned 251 * value is either one of the \e C UCUL_RED, \e CUCUL_BLACK etc. predefined252 * colours, or the special value \e C UCUL_DEFAULT meaning the media's253 * default foreground value, or the special value \e C UCUL_TRANSPARENT.251 * value is either one of the \e CACA_RED, \e CACA_BLACK etc. predefined 252 * colours, or the special value \e CACA_DEFAULT meaning the media's 253 * default foreground value, or the special value \e CACA_TRANSPARENT. 254 254 * 255 255 * If the attribute has ARGB colours, the nearest colour is returned. … … 261 261 * \return The corresponding ANSI foreground value. 262 262 */ 263 uint8_t c ucul_attr_to_ansi_fg(uint32_t attr)263 uint8_t caca_attr_to_ansi_fg(uint32_t attr) 264 264 { 265 265 return nearest_ansi(((uint16_t)attr >> 4) & 0x3fff); … … 269 269 * 270 270 * Get the ANSI background colour value for a given attribute. The returned 271 * value is either one of the \e C UCUL_RED, \e CUCUL_BLACK etc. predefined272 * colours, or the special value \e C UCUL_DEFAULT meaning the media's273 * default background value, or the special value \e C UCUL_TRANSPARENT.271 * value is either one of the \e CACA_RED, \e CACA_BLACK etc. predefined 272 * colours, or the special value \e CACA_DEFAULT meaning the media's 273 * default background value, or the special value \e CACA_TRANSPARENT. 274 274 * 275 275 * If the attribute has ARGB colours, the nearest colour is returned. … … 281 281 * \return The corresponding ANSI background value. 282 282 */ 283 uint8_t c ucul_attr_to_ansi_bg(uint32_t attr)283 uint8_t caca_attr_to_ansi_bg(uint32_t attr) 284 284 { 285 285 return nearest_ansi(attr >> 18); … … 301 301 * \return The corresponding 12-bit RGB foreground value. 302 302 */ 303 uint16_t c ucul_attr_to_rgb12_fg(uint32_t attr)303 uint16_t caca_attr_to_rgb12_fg(uint32_t attr) 304 304 { 305 305 uint16_t fg = (attr >> 4) & 0x3fff; … … 308 308 return ansitab16[fg ^ 0x40] & 0x0fff; 309 309 310 if(fg == (C UCUL_DEFAULT | 0x40))311 return ansitab16[C UCUL_LIGHTGRAY] & 0x0fff;312 313 if(fg == (C UCUL_TRANSPARENT | 0x40))314 return ansitab16[C UCUL_LIGHTGRAY] & 0x0fff;310 if(fg == (CACA_DEFAULT | 0x40)) 311 return ansitab16[CACA_LIGHTGRAY] & 0x0fff; 312 313 if(fg == (CACA_TRANSPARENT | 0x40)) 314 return ansitab16[CACA_LIGHTGRAY] & 0x0fff; 315 315 316 316 return (fg << 1) & 0x0fff; … … 332 332 * \return The corresponding 12-bit RGB background value. 333 333 */ 334 uint16_t c ucul_attr_to_rgb12_bg(uint32_t attr)334 uint16_t caca_attr_to_rgb12_bg(uint32_t attr) 335 335 { 336 336 uint16_t bg = attr >> 18; … … 339 339 return ansitab16[bg ^ 0x40] & 0x0fff; 340 340 341 if(bg == (C UCUL_DEFAULT | 0x40))342 return ansitab16[C UCUL_BLACK] & 0x0fff;343 344 if(bg == (C UCUL_TRANSPARENT | 0x40))345 return ansitab16[C UCUL_BLACK] & 0x0fff;341 if(bg == (CACA_DEFAULT | 0x40)) 342 return ansitab16[CACA_BLACK] & 0x0fff; 343 344 if(bg == (CACA_TRANSPARENT | 0x40)) 345 return ansitab16[CACA_BLACK] & 0x0fff; 346 346 347 347 return (bg << 1) & 0x0fff; … … 367 367 * \param argb An array of 8-bit integers. 368 368 */ 369 void c ucul_attr_to_argb64(uint32_t attr, uint8_t argb[8])369 void caca_attr_to_argb64(uint32_t attr, uint8_t argb[8]) 370 370 { 371 371 uint16_t fg = (attr >> 4) & 0x3fff; … … 374 374 if(bg < (0x10 | 0x40)) 375 375 bg = ansitab16[bg ^ 0x40]; 376 else if(bg == (C UCUL_DEFAULT | 0x40))377 bg = ansitab16[C UCUL_BLACK];378 else if(bg == (C UCUL_TRANSPARENT | 0x40))376 else if(bg == (CACA_DEFAULT | 0x40)) 377 bg = ansitab16[CACA_BLACK]; 378 else if(bg == (CACA_TRANSPARENT | 0x40)) 379 379 bg = 0x0fff; 380 380 else … … 388 388 if(fg < (0x10 | 0x40)) 389 389 fg = ansitab16[fg ^ 0x40]; 390 else if(fg == (C UCUL_DEFAULT | 0x40))391 fg = ansitab16[C UCUL_LIGHTGRAY];392 else if(fg == (C UCUL_TRANSPARENT | 0x40))390 else if(fg == (CACA_DEFAULT | 0x40)) 391 fg = ansitab16[CACA_LIGHTGRAY]; 392 else if(fg == (CACA_TRANSPARENT | 0x40)) 393 393 fg = 0x0fff; 394 394 else … … 412 412 return argb14 ^ 0x40; 413 413 414 if(argb14 == (C UCUL_DEFAULT | 0x40) || argb14 == (CUCUL_TRANSPARENT | 0x40))414 if(argb14 == (CACA_DEFAULT | 0x40) || argb14 == (CACA_TRANSPARENT | 0x40)) 415 415 return argb14 ^ 0x40; 416 416 417 417 if(argb14 < 0x0fff) /* too transparent */ 418 return C UCUL_TRANSPARENT;419 420 best = C UCUL_DEFAULT;418 return CACA_TRANSPARENT; 419 420 best = CACA_DEFAULT; 421 421 dist = 0x3fff; 422 422 for(i = 0; i < 16; i++) … … 452 452 | ((uint32_t)(i & 0x00f) * 0x000011)) 453 453 454 uint32_t _c ucul_attr_to_rgb24fg(uint32_t attr)455 { 456 return RGB12TO24(c ucul_attr_to_rgb12_fg(attr));457 } 458 459 uint32_t _c ucul_attr_to_rgb24bg(uint32_t attr)460 { 461 return RGB12TO24(c ucul_attr_to_rgb12_bg(attr));462 } 463 454 uint32_t _caca_attr_to_rgb24fg(uint32_t attr) 455 { 456 return RGB12TO24(caca_attr_to_rgb12_fg(attr)); 457 } 458 459 uint32_t _caca_attr_to_rgb24bg(uint32_t attr) 460 { 461 return RGB12TO24(caca_attr_to_rgb12_bg(attr)); 462 } 463
Note: See TracChangeset
for help on using the changeset viewer.