Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uint8ClampedList in Dart

Tags:

list

dart

clamp

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.

like image 661
jcklie Avatar asked May 30 '14 23:05

jcklie


1 Answers

"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.

like image 138
lrn Avatar answered Sep 27 '22 17:09

lrn