问题
I've got a HSB color problem and don't know how to calculate it in the right way.
I've got Hue of a color between 360
and 0
and if i substrate -1
from 0
i get the value -1
and not 360
. I'think i've to do something with modulo to get the right values ?! so my problem is to convert a multiple of a unit to a number between 0 and 360. Could someone help me?
回答1:
A negative hue value is equivalent to 360 + value
, that means -10 is equivalent to 350. To achieve that behavior do:
var val = -10; // or whatever your value is
var hue = (val + 360) % 360 // --> 350
That works also correctly with positive values, val = 380 --> hue = 20. If you are dealing with values greater than +/- 360 simply add 720
(or 1080, 1440, ...) instead of 360
before doing % 360.
We can wrap it in a handy function that whatever you input always returns a value between 0 and 360:
function adjustHue(val) {
if (val < 0) val += Math.ceil(-val / 360) * 360;
return val % 360;
}
来源:https://stackoverflow.com/questions/26703470/colors-h-s-b-from-360-to-0-and-from-0-to-360-and-from-360-to-720-and