File: WebKitColor.cpp

package info (click to toggle)
wpewebkit 2.48.3-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 421,720 kB
  • sloc: cpp: 3,670,389; javascript: 194,411; ansic: 165,592; python: 46,476; asm: 19,276; ruby: 18,528; perl: 16,602; xml: 4,650; yacc: 2,360; java: 1,993; sh: 1,948; lex: 1,327; pascal: 366; makefile: 85
file content (118 lines) | stat: -rw-r--r-- 3,489 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
 * Copyright (C) 2019 Igalia S.L.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this library; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#include "config.h"
#include "WebKitColor.h"

#include "WebKitColorPrivate.h"
#include <WebCore/CSSParser.h>

/**
 * WebKitColor:
 * @red: Red channel, between 0.0 and 1.0 inclusive
 * @green: Green channel, between 0.0 and 1.0 inclusive
 * @blue: Blue channel, between 0.0 and 1.0 inclusive
 * @alpha: Alpha channel, between 0.0 and 1.0 inclusive
 *
 * Boxed type representing a RGBA color.
 *
 * Since: 2.24
 */

/**
 * webkit_color_copy:
 * @color: a #WebKitColor
 *
 * Make a copy of @color.
 *
 * Returns: (transfer full): A copy of passed in #WebKitColor.
 *
 * Since: 2.24
 */
WebKitColor* webkit_color_copy(WebKitColor* color)
{
    g_return_val_if_fail(color, nullptr);

    WebKitColor* copy = static_cast<WebKitColor*>(fastZeroedMalloc(sizeof(WebKitColor)));
    copy->red = color->red;
    copy->green = color->green;
    copy->blue = color->blue;
    copy->alpha = color->alpha;
    return copy;
}

/**
 * webkit_color_free:
 * @color: a #WebKitColor
 *
 * Free the #WebKitColor.
 *
 * Since: 2.24
 */
void webkit_color_free(WebKitColor* color)
{
    g_return_if_fail(color);

    fastFree(color);
}

G_DEFINE_BOXED_TYPE(WebKitColor, webkit_color, webkit_color_copy, webkit_color_free);

const WebCore::Color webkitColorToWebCoreColor(WebKitColor* color)
{
    return WebCore::convertColor<WebCore::SRGBA<uint8_t>>(WebCore::SRGBA<float> { static_cast<float>(color->red), static_cast<float>(color->green), static_cast<float>(color->blue), static_cast<float>(color->alpha) });
}

void webkitColorFillFromWebCoreColor(const WebCore::Color& webCoreColor, WebKitColor* color)
{
    RELEASE_ASSERT(webCoreColor.isValid());

    auto [r, g, b, a] = webCoreColor.toColorTypeLossy<WebCore::SRGBA<float>>().resolved();
    color->red = r;
    color->green = g;
    color->blue = b;
    color->alpha = a;
}

/**
 * webkit_color_parse:
 * @color: a #WebKitColor to fill in
 * @color_string: color representation as color nickname or HEX string
 *
 * Create a new #WebKitColor for the given @color_string
 * representation. There are two valid representation types: standard color
 * names (see https://htmlcolorcodes.com/color-names/ for instance) or HEX
 * values.
 *
 * Returns: a #gboolean indicating if the @color was correctly filled in or not.
 *
 * Since: 2.24
 */
gboolean webkit_color_parse(WebKitColor* color, const gchar* colorString)
{
    g_return_val_if_fail(color, FALSE);
    g_return_val_if_fail(colorString, FALSE);

    auto webCoreColor = WebCore::CSSParser::parseColorWithoutContext(String::fromLatin1(colorString));
    if (!webCoreColor.isValid())
        return FALSE;

    webkitColorFillFromWebCoreColor(webCoreColor, color);
    return TRUE;
}