Uint8ClampedList in Dart
Asked Answered
G

1

5

I am curently playing with Dart and especially dart:typed_data. I stumbled across a class where I have no idea what its purpose/speciality is. I speak of Uint8ClampedList. The difference to the Uint8List in the documentation is the sentence

Indexed store clamps the value to range 0..0xFF.

What does that sentence actually mean? Why does this class exist? I am really curious.

Grizzly answered 30/5, 2014 at 23:41 Comment(0)
E
7

"Clamping" means that values below 0 become 0 and values above 0xff become 0xff when stored into the Uint8ClampedList. Any value in the range 0..0xff can be stored into the list without change.

This differs from other typed lists where the value is instead just truncated to the low 8 (or 16 or 32) bits.

The clamped list (and the name itself) mirrors the Uint8ClampedArray of JavaScript.

One usage of clamping that I have seen is for RGB(A) color images, where really over-saturated colors (e.g., and R value > 255) would be capped at the maximum value instead of wrapping around and becoming dark. It allows you to make some transformations on the values without having to care about handling overflow. See the Uint8ClampedArray specification - it was introduced to have an array type matching the behavior of an existing canvas type.

Embraceor answered 31/5, 2014 at 0:5 Comment(1)
Thank you for the answer, that explains its existence. But why does that class exist in JavaScript and what are typical use cases?Grizzly

© 2022 - 2024 — McMap. All rights reserved.