0

In ActionScript 3, is there a shorter way to clip a value to be between 0 and 255?

value = Math.min(255, Math.max(0, value))
Bemmu
  • 17,849
  • 16
  • 76
  • 93
  • Thats my old answer for checking values between others : http://stackoverflow.com/questions/7864858/how-to-check-if-a-number-is-between-two-other-numbers-in-actionscript-3/7864948#7864948 – turbosqel Aug 29 '12 at 20:04

2 Answers2

1

Nope, you could create your own shortcut function easily enough though:

function clip(val:Number, min:Number, max:Number):Number {
    return Math.min(max, Math.max(min, val));
}
BadFeelingAboutThis
  • 14,445
  • 2
  • 33
  • 40
1

If you're looking for something fast, the following will do:

k = (k | -int(k > 256)) & -int(k > 0) & 0xFF;
cleong
  • 7,242
  • 4
  • 31
  • 40
  • while I agree it's the most efficient way, it's not really an answer to the question... – BadFeelingAboutThis Aug 30 '12 at 05:44
  • Right. I was just checking if there might be something like Math.clip(0, 255, value) or other way, but since there wasn't I accepted the answer that I felt contributed the most. – Bemmu Aug 30 '12 at 06:09