从360到0和从0到(-360)和从(-360)到(-720)的颜色(h,s,b)等等 [英] Colors (h,s,b) from 360 to 0 and from 0 to (-360) and from (-360) to (-720) and so on
问题描述
我有一个HSB颜色问题,不知道如何以正确的方式计算。
I've got a HSB color problem and don't know how to calculate it in the right way.
我有一个颜色在 360
和 0
并且如果i substrate -1
从 0
我得到值 -1
而不是 360
。我想我要做一些模数来获得正确的值?所以我的问题是将单位的倍数转换为0和360之间的数字。有人可以帮助我吗?
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?
推荐答案
负色调值相当于 360 + value
意味着-10等于350.要实现那个行为,请执行:
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
这对正值也正确,val = 380 - > hue =正在处理大于+/- 360的值,只需添加 720
(或1080,1440,...),而不是 360 $ c $
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.
我们可以将它包装在一个方便的函数中,无论你输入什么,总是返回一个介于0和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;
}
这篇关于从360到0和从0到(-360)和从(-360)到(-720)的颜色(h,s,b)等等的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!