1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "mozilla/StyleColorInlines.h"
9 #include "mozilla/ComputedStyle.h"
10 #include "mozilla/ComputedStyleInlines.h"
11 #include "mozilla/dom/BindingDeclarations.h"
13 #include "nsStyleStruct.h"
18 bool StyleColor::MaybeTransparent() const {
19 // We know that the color is opaque when it's a numeric color with
21 return !IsAbsolute() || AsAbsolute().alpha
!= 1.0f
;
25 StyleAbsoluteColor
StyleColor::ResolveColor(
26 const StyleAbsoluteColor
& aForegroundColor
) const {
31 if (IsCurrentColor()) {
32 return aForegroundColor
;
35 MOZ_ASSERT(IsColorMix(), "should be the only type left at this point.");
36 return Servo_ResolveColor(this, &aForegroundColor
);
40 nscolor
StyleColor::CalcColor(nscolor aColor
) const {
41 return ResolveColor(StyleAbsoluteColor::FromColor(aColor
)).ToColor();
45 nscolor
StyleColor::CalcColor(
46 const StyleAbsoluteColor
& aForegroundColor
) const {
47 return ResolveColor(aForegroundColor
).ToColor();
51 nscolor
StyleColor::CalcColor(const ComputedStyle
& aStyle
) const {
52 return ResolveColor(aStyle
.StyleText()->mColor
).ToColor();
56 nscolor
StyleColor::CalcColor(const nsIFrame
* aFrame
) const {
57 return ResolveColor(aFrame
->StyleText()->mColor
).ToColor();
60 StyleAbsoluteColor
StyleAbsoluteColor::ToColorSpace(
61 StyleColorSpace aColorSpace
) const {
62 return Servo_ConvertColorSpace(this, aColorSpace
);
65 nscolor
StyleAbsoluteColor::ToColor() const {
66 auto srgb
= ToColorSpace(StyleColorSpace::Srgb
);
68 // TODO(tlouw): Needs gamut mapping here. Right now we just hard clip the
69 // components to [0..1], which will yield invalid colors.
70 // https://bugzilla.mozilla.org/show_bug.cgi?id=1626624
71 auto red
= std::clamp(srgb
.components
._0
, 0.0f
, 1.0f
);
72 auto green
= std::clamp(srgb
.components
._1
, 0.0f
, 1.0f
);
73 auto blue
= std::clamp(srgb
.components
._2
, 0.0f
, 1.0f
);
75 return NS_RGBA(nsStyleUtil::FloatToColorComponent(red
),
76 nsStyleUtil::FloatToColorComponent(green
),
77 nsStyleUtil::FloatToColorComponent(blue
),
78 nsStyleUtil::FloatToColorComponent(srgb
.alpha
));
81 } // namespace mozilla