tor-browser

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

values.rs (1263B)


      1 /* This Source Code Form is subject to the terms of the Mozilla Public
      2 * License, v. 2.0. If a copy of the MPL was not distributed with this
      3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
      4 
      5 #![allow(unsafe_code)]
      6 
      7 //! Different kind of helpers to interact with Gecko values.
      8 
      9 use crate::color::{AbsoluteColor, ColorSpace};
     10 
     11 /// Convert a color value to `nscolor`.
     12 pub fn convert_absolute_color_to_nscolor(color: &AbsoluteColor) -> u32 {
     13    let srgb = color.to_color_space(ColorSpace::Srgb);
     14    u32::from_le_bytes([
     15        (srgb.components.0 * 255.0).round() as u8,
     16        (srgb.components.1 * 255.0).round() as u8,
     17        (srgb.components.2 * 255.0).round() as u8,
     18        (srgb.alpha * 255.0).round() as u8,
     19    ])
     20 }
     21 
     22 /// Convert a given `nscolor` to a Servo AbsoluteColor value.
     23 pub fn convert_nscolor_to_absolute_color(color: u32) -> AbsoluteColor {
     24    let [r, g, b, a] = color.to_le_bytes();
     25    AbsoluteColor::srgb_legacy(r, g, b, a as f32 / 255.0)
     26 }
     27 
     28 #[test]
     29 fn convert_ns_color_to_absolute_color_should_be_in_legacy_syntax() {
     30    use crate::color::ColorFlags;
     31 
     32    let result = convert_nscolor_to_absolute_color(0x336699CC);
     33    assert!(result.flags.contains(ColorFlags::IS_LEGACY_SRGB));
     34 
     35    assert!(result.is_legacy_syntax());
     36 }