Files
react-native/packages/react-native/ReactCommon/react/renderer/css/CSSValue.h
T
Jorge Cabiedes Acosta 417ff5fec9 Add <hex-color> to CSSParser (#44624)
Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/44624

The syntax for <color> is defined as (https://www.w3.org/TR/css-color-4/#color-syntax):

```
<color> = <color-base> | currentColor | <system-color>

<color-base> = <hex-color> | <color-function> | <named-color> | transparent
<color-function> = <rgb()> | <rgba()> | <hsl()> | <hsla()> | <hwb()> | <lab()> | <lch()> | <oklab()> | <oklch()> | <color()>
```

This diff implements in particular:
```
<color-base> = <hex-color>
```

We parse over a `<hash-token>` to extract a 3, 4, 6 or 8 digit long hexadecimal and parse it into a CSSColor struct representation:

```
struct Color {
    uint8_t r // 0-255
    uint8_t g // 0-255
    uint8_t b // 0-255
    uint8_t a // 0.0 - 1.0
}
```
Changelog: [Internal]

Reviewed By: NickGerleman

Differential Revision: D57287309

fbshipit-source-id: 35ce82e52fdc8fc86425e03d7883ae09b429b9e4
2024-05-20 07:29:51 -07:00

93 lines
1.8 KiB
C++

/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#pragma once
#include <cstdint>
#include <type_traits>
#include <react/renderer/css/CSSKeywords.h>
#include <react/renderer/css/CSSLengthUnit.h>
namespace facebook::react {
/**
* Represents a CSS component value type.
* https://www.w3.org/TR/css-values-4/#component-types
*/
enum class CSSValueType : uint8_t {
CSSWideKeyword,
Keyword,
Length,
Number,
Percentage,
Ratio,
Angle,
Color,
};
/**
* Concrete representation for a CSS basic data type, or keywords
* https://www.w3.org/TR/css-values-4/#component-types
*/
template <typename T>
concept CSSDataType = std::is_trivially_destructible_v<T> &&
std::is_copy_constructible_v<T> && std::is_default_constructible_v<T>;
#pragma pack(push, 1)
/**
* Representation of CSS <length> data type
* https://www.w3.org/TR/css-values-4/#lengths
*/
struct CSSLength {
float value{};
CSSLengthUnit unit{CSSLengthUnit::Px};
};
#pragma pack(pop)
/**
* Representation of CSS <percentage> data type
* https://www.w3.org/TR/css-values-4/#percentages
*/
struct CSSPercentage {
float value{};
};
/**
* Representation of CSS <number> data type
* https://www.w3.org/TR/css-values-4/#numbers
*/
struct CSSNumber {
float value{};
};
/**
* Representation of CSS <ratio> data type
* https://www.w3.org/TR/css-values-4/#ratios
*/
struct CSSRatio {
float numerator{};
float denominator{};
};
/**
* Representation of CSS <angle> data type
* https://www.w3.org/TR/css-values-4/#angles
*/
struct CSSAngle {
float degrees{};
};
struct CSSColor {
uint8_t r{};
uint8_t g{};
uint8_t b{};
uint8_t a{};
};
} // namespace facebook::react