Skip to content

Commit 74b8701

Browse files
committed
Use Gecko's behavior for calculating percent colors
1 parent af83617 commit 74b8701

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22

33
name = "cssparser"
4-
version = "0.13.1"
4+
version = "0.13.2"
55
authors = [ "Simon Sapin <simon.sapin@exyr.org>" ]
66

77
description = "Rust implementation of CSS Syntax Level 3"

src/color.rs

+11-3
Original file line numberDiff line numberDiff line change
@@ -386,13 +386,21 @@ fn parse_color_hash(value: &str) -> Result<Color, ()> {
386386
}
387387

388388
fn clamp_unit_f32(val: f32) -> u8 {
389-
// Scale by 256, not 255, so that each of the 256 u8 values has an equal range
390-
// of f32 values mapping to it. Floor before clamping.
389+
// Whilst scaling by 256 and flooring would provide
390+
// an equal distribution of integers to percentage inputs,
391+
// this is not what Gecko does so we instead multiply by 255
392+
// and round (adding 0.5 and flooring is equivalent to rounding)
391393
//
392-
// Clamping to 256 and flooring after would let 1.0 map to 256, and
394+
// Chrome does something similar for the alpha value, but not
395+
// the rgb values.
396+
//
397+
// See https://bugzilla.mozilla.org/show_bug.cgi?id=1340484
398+
//
399+
// Clamping to 256 and rounding after would let 1.0 map to 256, and
393400
// `256.0_f32 as u8` is undefined behavior:
394401
//
395402
// https://github.com/rust-lang/rust/issues/10184
403+
clamp_256_f32(val * 255. + 0.5)
396404
clamp_256_f32(val * 256.)
397405
}
398406

0 commit comments

Comments
 (0)