tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

StyleColor.cpp (2531B)


      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/. */
      6 
      7 #include "mozilla/ComputedStyle.h"
      8 #include "mozilla/ComputedStyleInlines.h"
      9 #include "mozilla/StyleColorInlines.h"
     10 #include "mozilla/dom/BindingDeclarations.h"
     11 #include "nsIFrame.h"
     12 #include "nsStyleStruct.h"
     13 
     14 namespace mozilla {
     15 
     16 template <>
     17 bool StyleColor::MaybeTransparent() const {
     18  // We know that the color is opaque when it's a numeric color with
     19  // alpha == 1.0.
     20  return !IsAbsolute() || AsAbsolute().alpha != 1.0f;
     21 }
     22 
     23 template <>
     24 StyleAbsoluteColor StyleColor::ResolveColor(
     25    const StyleAbsoluteColor& aForegroundColor) const {
     26  if (IsAbsolute()) {
     27    return AsAbsolute();
     28  }
     29 
     30  if (IsCurrentColor()) {
     31    return aForegroundColor;
     32  }
     33 
     34  return Servo_ResolveColor(this, &aForegroundColor);
     35 }
     36 
     37 template <>
     38 nscolor StyleColor::CalcColor(nscolor aColor) const {
     39  return ResolveColor(StyleAbsoluteColor::FromColor(aColor)).ToColor();
     40 }
     41 
     42 template <>
     43 nscolor StyleColor::CalcColor(
     44    const StyleAbsoluteColor& aForegroundColor) const {
     45  return ResolveColor(aForegroundColor).ToColor();
     46 }
     47 
     48 template <>
     49 nscolor StyleColor::CalcColor(const ComputedStyle& aStyle) const {
     50  return ResolveColor(aStyle.StyleText()->mColor).ToColor();
     51 }
     52 
     53 template <>
     54 nscolor StyleColor::CalcColor(const nsIFrame* aFrame) const {
     55  return ResolveColor(aFrame->StyleText()->mColor).ToColor();
     56 }
     57 
     58 StyleAbsoluteColor StyleAbsoluteColor::ToColorSpace(
     59    StyleColorSpace aColorSpace) const {
     60  return Servo_ConvertColorSpace(this, aColorSpace);
     61 }
     62 
     63 nscolor StyleAbsoluteColor::ToColor() const {
     64  auto srgb = ToColorSpace(StyleColorSpace::Srgb);
     65 
     66  // TODO(tlouw): Needs gamut mapping here. Right now we just hard clip the
     67  //              components to [0..1], which will yield invalid colors.
     68  //              https://bugzilla.mozilla.org/show_bug.cgi?id=1626624
     69  auto red = std::clamp(srgb.components._0, 0.0f, 1.0f);
     70  auto green = std::clamp(srgb.components._1, 0.0f, 1.0f);
     71  auto blue = std::clamp(srgb.components._2, 0.0f, 1.0f);
     72 
     73  return NS_RGBA(nsStyleUtil::FloatToColorComponent(red),
     74                 nsStyleUtil::FloatToColorComponent(green),
     75                 nsStyleUtil::FloatToColorComponent(blue),
     76                 nsStyleUtil::FloatToColorComponent(srgb.alpha));
     77 }
     78 
     79 }  // namespace mozilla