source: mainline/uspace/lib/gfx/src/color.c

Last change on this file was 211fd68, checked in by Jiri Svoboda <jiri@…>, 16 months ago

Add text mode support to Barber

  • Property mode set to 100644
File size: 3.4 KB
Line 
1/*
2 * Copyright (c) 2024 Jiri Svoboda
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup libgfx
30 * @{
31 */
32/**
33 * @file Color operations
34 */
35
36#include <gfx/color.h>
37#include <stdint.h>
38#include <stdlib.h>
39#include "../private/color.h"
40
41/** Create new 16-bit per channel RGB color.
42 *
43 * Create a new RGB color where the R, G, B components have 16 bits
44 * of precision each.
45 *
46 * @param r Red component
47 * @param g Green component
48 * @param b Blue component
49 * @param rcolor Place to store pointer to new color
50 *
51 * @return EOK on success or an error code, ENOMEM if out of resources,
52 * EIO if the graphic device connection was lost
53 */
54errno_t gfx_color_new_rgb_i16(uint16_t r, uint16_t g, uint16_t b,
55 gfx_color_t **rcolor)
56{
57 gfx_color_t *color;
58
59 color = calloc(1, sizeof(gfx_color_t));
60 if (color == NULL)
61 return ENOMEM;
62
63 color->r = r;
64 color->g = g;
65 color->b = b;
66 color->attr = 0xff;
67
68 *rcolor = color;
69 return EOK;
70}
71
72/** Create new EGA color.
73 *
74 * @param attr EGA attributes
75 * @param rcolor Place to store pointer to new color
76 *
77 * @return EOK on success or an error code, ENOMEM if out of resources,
78 * EIO if the graphic device connection was lost
79 */
80errno_t gfx_color_new_ega(uint8_t attr, gfx_color_t **rcolor)
81{
82 gfx_color_t *color;
83
84 color = calloc(1, sizeof(gfx_color_t));
85 if (color == NULL)
86 return ENOMEM;
87
88 color->attr = attr;
89
90 *rcolor = color;
91 return EOK;
92}
93
94/** Delete color.
95 *
96 * @param color Color
97 */
98void gfx_color_delete(gfx_color_t *color)
99{
100 free(color);
101}
102
103/** Convert color to 16-bit RGB coordinates.
104 *
105 * @param color Color
106 * @param r Place to store red coordinate
107 * @param g Place to store green coordinate
108 * @param b Place to store blue coordinate
109 */
110void gfx_color_get_rgb_i16(gfx_color_t *color, uint16_t *r, uint16_t *g,
111 uint16_t *b)
112{
113 *r = color->r;
114 *g = color->g;
115 *b = color->b;
116}
117
118/** Convert color to EGA attributes.
119 *
120 * @param color Color
121 * @param attr Place to store EGA attributes
122 */
123void gfx_color_get_ega(gfx_color_t *color, uint8_t *attr)
124{
125 *attr = color->attr;
126}
127
128/** @}
129 */
Note: See TracBrowser for help on using the repository browser.