Colors (h,s,b) from 360 to 0 and from 0 to (-360) and from (-360) to (-720) and so on

≯℡__Kan透↙ 提交于 2019-12-25 03:31:24

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!