2020-08-26 01:57:08 +02:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var ToNumber = require('./ToNumber');
|
2021-02-26 04:58:33 +01:00
|
|
|
var floor = require('./floor');
|
2020-08-26 01:57:08 +02:00
|
|
|
|
|
|
|
var $isNaN = require('../helpers/isNaN');
|
|
|
|
|
2021-02-26 04:58:33 +01:00
|
|
|
// https://ecma-international.org/ecma-262/6.0/#sec-touint8clamp
|
2020-08-26 01:57:08 +02:00
|
|
|
|
|
|
|
module.exports = function ToUint8Clamp(argument) {
|
|
|
|
var number = ToNumber(argument);
|
|
|
|
if ($isNaN(number) || number <= 0) { return 0; }
|
|
|
|
if (number >= 0xFF) { return 0xFF; }
|
2021-02-26 04:58:33 +01:00
|
|
|
var f = floor(argument);
|
2020-08-26 01:57:08 +02:00
|
|
|
if (f + 0.5 < number) { return f + 1; }
|
|
|
|
if (number < f + 0.5) { return f; }
|
|
|
|
if (f % 2 !== 0) { return f + 1; }
|
|
|
|
return f;
|
|
|
|
};
|