source: mainline/uspace/lib/ui/src/paint.c@ ba09d06

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since ba09d06 was de9992c, checked in by Jiri Svoboda <jiri@…>, 5 years ago

Factor out bevel drawing, store button colors in ui_resource_t

  • Property mode set to 100644
File size: 3.4 KB
Line 
1/*
2 * Copyright (c) 2020 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 libui
30 * @{
31 */
32/**
33 * @file Painting routines
34 */
35
36#include <errno.h>
37#include <gfx/color.h>
38#include <gfx/context.h>
39#include <gfx/render.h>
40#include <ui/paint.h>
41
42/** Paint bevel.
43 *
44 * @param gc Graphic context
45 * @param rect Rectangle to paint into
46 * @param tlcolor Top-left color
47 * @param brcolor Bottom-right color
48 * @param thickness Bevel thickness in pixels
49 * @param inside Place to store rectangle of the interior or @c NULL
50 * @reutrn EOK on success or an error code
51 */
52errno_t ui_paint_bevel(gfx_context_t *gc, gfx_rect_t *rect,
53 gfx_color_t *tlcolor, gfx_color_t *brcolor, gfx_coord_t thickness,
54 gfx_rect_t *inside)
55{
56 gfx_rect_t frect;
57 gfx_coord_t i;
58 errno_t rc;
59
60 /* Top left */
61
62 rc = gfx_set_color(gc, tlcolor);
63 if (rc != EOK)
64 goto error;
65
66 for (i = 0; i < thickness; i++) {
67 frect.p0.x = rect->p0.x + i;
68 frect.p0.y = rect->p0.y + i;
69 frect.p1.x = rect->p1.x - i - 1;
70 frect.p1.y = rect->p0.y + i + 1;
71 rc = gfx_fill_rect(gc, &frect);
72 if (rc != EOK)
73 goto error;
74
75 frect.p0.x = rect->p0.x + i;
76 frect.p0.y = rect->p0.y + i + 1;
77 frect.p1.x = rect->p0.x + i + 1;
78 frect.p1.y = rect->p1.y - i - 1;
79 rc = gfx_fill_rect(gc, &frect);
80 if (rc != EOK)
81 goto error;
82 }
83
84 /* Bottom right */
85
86 rc = gfx_set_color(gc, brcolor);
87 if (rc != EOK)
88 goto error;
89
90 for (i = 0; i < thickness; i++) {
91 frect.p0.x = rect->p0.x + i;
92 frect.p0.y = rect->p1.y - i - 1;
93 frect.p1.x = rect->p1.x - i - 1;
94 frect.p1.y = rect->p1.y - i;
95 rc = gfx_fill_rect(gc, &frect);
96 if (rc != EOK)
97 goto error;
98
99 frect.p0.x = rect->p1.x - i - 1;
100 frect.p0.y = rect->p0.y + i;
101 frect.p1.x = rect->p1.x - i;
102 frect.p1.y = rect->p1.y - i;
103 rc = gfx_fill_rect(gc, &frect);
104 if (rc != EOK)
105 goto error;
106 }
107
108 if (inside != NULL) {
109 inside->p0.x = rect->p0.x + thickness;
110 inside->p0.y = rect->p0.y + thickness;
111 inside->p1.x = rect->p1.x - thickness;
112 inside->p1.y = rect->p1.y - thickness;
113 }
114
115 return EOK;
116error:
117 return rc;
118}
119
120/** @}
121 */
Note: See TracBrowser for help on using the repository browser.