Skip to content

Commit

Permalink
karm-style: Implemented rgb() and rgba().
Browse files Browse the repository at this point in the history
  • Loading branch information
sleepy-monax committed Oct 2, 2024
1 parent 1cdd44f commit 7168dd9
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 0 deletions.
56 changes: 56 additions & 0 deletions src/vaev-style/values.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "values.h"

#include "base.h"

namespace Vaev::Style {

// MARK: Parser ----------------------------------------------------------------
Expand Down Expand Up @@ -178,6 +180,58 @@ static Res<Gfx::Color> _parseHexColor(Io::SScan &s) {
}
}

static Res<Gfx::Color> _parseFuncColor(Css::Sst const &s) {
logDebug("parse func color: {}", s);
if (s.prefix == Css::Token::function("rgb(")) {
Cursor<Css::Sst> scan = s.content;

eatWhitespace(scan);
auto r = try$(parseValue<Integer>(scan));
eatWhitespace(scan);

scan.skip(Css::Token::COMMA);

eatWhitespace(scan);
auto g = try$(parseValue<Integer>(scan));
eatWhitespace(scan);

scan.skip(Css::Token::COMMA);

eatWhitespace(scan);
auto b = try$(parseValue<Integer>(scan));
eatWhitespace(scan);

return Ok(Gfx::Color::fromRgb(r, g, b));
} else if (s.prefix == Css::Token::function("rgba(")) {
Cursor<Css::Sst> scan = s.content;

eatWhitespace(scan);
auto r = try$(parseValue<Integer>(scan));

eatWhitespace(scan);
scan.skip(Css::Token::COMMA);
eatWhitespace(scan);

auto g = try$(parseValue<Integer>(scan));

eatWhitespace(scan);
scan.skip(Css::Token::COMMA);
eatWhitespace(scan);

auto b = try$(parseValue<Integer>(scan));

eatWhitespace(scan);
scan.skip(Css::Token::COMMA);
eatWhitespace(scan);

auto a = try$(parseValue<Number>(scan));

return Ok(Gfx::Color::fromRgba(r, g, b, 255 * a));
} else {
return Error::invalidData("unknown color function");
}
}

Res<Color> ValueParser<Color>::parse(Cursor<Css::Sst> &c) {
if (c.ended())
return Error::invalidData("unexpected end of input");
Expand Down Expand Up @@ -210,6 +264,8 @@ Res<Color> ValueParser<Color>::parse(Cursor<Css::Sst> &c) {
c.next();
return Ok(TRANSPARENT);
}
} else if (c.peek() == Css::Sst::FUNC) {
return Ok(try$(_parseFuncColor(c.next())));
}

return Error::invalidData("expected color");
Expand Down
54 changes: 54 additions & 0 deletions tests/css/colors/colors.xhtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<test id="color-red">
<container>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">

<head>
<style>
body {
margin: 0px;
}

section {
width: 200px;
height: 200px;
background: #ddd;
position: relative;
}

div {
width: 100px;
height: 100px;
}
</style>
</head>

<body>
<section>
<slot />
</section>
</body>

</html>
</container>

<rendering>
<div style="background-color: red;" />
</rendering>

<rendering>
<div style="background-color: #f00;" />
</rendering>

<rendering>
<div style="background-color: rgb(255, 0, 0);" />
</rendering>

<rendering>
<div style="background-color: rgba(255, 0, 0, 1);" />
</rendering>

<error>
</error>


</test>

0 comments on commit 7168dd9

Please sign in to comment.