像艺术课一样混合颜色(添加和减去颜色)!
我正在构建一个颜色类,并且希望添加更多操作(颜色、百分比)和少(颜色,百分比)。这需要能够添加和减去颜色,而我在算术方面遇到了困难。如何使用 RGB 或 HSB(HSV) 或 HEX 执行以下操作:
操作 - echo color('blue')->more('yellow', 100%);
< /strong>
- 蓝色 + 黄色 = 绿色
或
操作 - echo color('blue')->more('yellow', 50%);
- blue + .5 *黄色=深绿色
对于减法我有一个非常模糊的概念:
操作 - echo color('orange-yellow')->less('red', 50%);
- 橙黄色 - .5 * 红色 = 黄色
编辑: 好的,感谢您迄今为止的投入。我尝试过将 CYM 添加到彼此,不幸的是 CYM 中的红色 (255, 0, 0) ~= (0, 1, 1) 然后如果将其添加到蓝色 (0, 0, 255) ~= (1, 1) , 0) 将等于 (1, 2, 1) 或 (1, 1, 1),在 CYM 中为黑色。
我使用色相饱和度得到了最接近的结果 亮度(HSB)。事实上它有效 每种颜色组合除了 红色搞乱了。我相信这是 因为红色是在开头并且 色调结束(色调使用度数 [0, 360])。
您的任何更多想法将不胜感激!
编辑2:
好的,经过一个晚上的闲逛后,这是一个我非常满意的“更多”方法。
它使用HSB(色相-饱和度-亮度)颜色模型,现在不要问我为什么CYM不起作用。我是一个色彩新手。看起来确实可行,因为这就是打印机混合颜色的方式。我非常喜欢 HSB 模型,而且当您使用颜色选择器时,Photoshop 会显示它。
我已将其添加为答案,所以请告诉我你们的想法!再次感谢!
任何帮助都会很棒!
谢谢, 马特
I'm building a color class and I'm looking to add operations more(color, percentage) & less(color, percentage). This requires being able to add and subtract colors and I'm having a hard time with the arithmetic. How do I use RGB or HSB(HSV) or HEX to do operations like:
Operation - echo color('blue')->more('yellow', 100%);
- blue + yellow = green
or
Operation - echo color('blue')->more('yellow', 50%);
- blue + .5 * yellow = dark-green
For subtracting I have a very vague notion of it:
Operation - echo color('orange-yellow')->less('red', 50%);
- orange-yellow - .5 * red = yellow
EDIT:
Okay thanks for your input so far. I've tried adding CYM to each other unfortunately red (255, 0, 0) ~= (0, 1, 1) in CYM and then if you add that onto blue (0, 0, 255) ~= (1, 1, 0) it will equal (1, 2, 1) or (1, 1, 1) which is black in CYM.
I got the closest using Hue Saturation
Brightness (HSB). In fact it works
with every color combination except
red messes up. I believe this is
because red is at the beginning and
end of hue (hue uses degrees [0,
360]).
Any more of your thoughts would be greatly appreciated!
EDIT 2:
Okay, so after an evening with messing around, this is a "more" method that I'm really happy with.
It uses the HSB (Hue-Saturation-Brightness) color model, Now don't ask me why I CYM didn't work. I'm a color newbie. It does seem like it would work seeing as thats how printers blend colors. I like the HSB model a lot, plus its what photoshop shows when you use the color picker.
I've added it as an answer, so let me know what you guys think! Thanks again!
Any help would be great!
Thanks,
Matt
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
使用 RGB 颜色空间的一种解决方案是在内部表示颜色 - 红色、绿色、值。当需要十六进制表示时,请根据当前值制作一个并将其发送回。
然后,
more
和less
方法只需操作当前的红色、绿色或蓝色值。转换为十六进制字符串 将
诸如
'orange-green'
之类的字符串转换为其 RGB 分量是一个有些不同的问题。One solution using the RGB color space is to internally represent the colors as such - red, green, value. When a hex representation is needed, then make one from the current values and send it back.
The
more
andless
methods then simply manipulate the current red, green, or blue values.Convert to a hex string as
Converting a string such as
'orange-green'
to it's RGB components is a somewhat different problem.有很多不同的混合颜色模型。我建议使用 RGBA 模型。
所以
mixColours(array($colors['opaque_green'], $colors['transparent_blue']);
应该给你一些浅绿色的 RGBA 十六进制字符串。对于单词转换,你可以将总数相加颜色中的浅色并推断颜色是“深色”、“正常”还是“浅色”。您还可以获得“绿色”、“橙色”等色调,并建立一个词汇来描述颜色。
There are a lot of different models for mixing colors. I would suggest the RGBA model.
So
mixColours(array($colors['opaque_green'], $colors['transparent_blue']);
should give you some RGBA hex string of an aqua color.For word conversion you could add up the total light in the color and deduce if the colour is 'dark', 'normal' or 'light'. You could also get the hue as 'green', 'orange' etc and build up a vocabulary to describe the color.
太有趣了!
示例:
您可以检查所有方法的源代码,简短版本是添加、删除、增加、减少、更多、更少 - 元素和颜色的完整链接,添加辅助函数 Color() 让事情变得更容易。
希望有帮助!
编辑:刚刚赶上了线程(完成此操作后),看到你想要 0xFFFF00 + 0x0000FF 来制作绿色,而不是白色 - 叹息这不会这样做,它只适用于十六进制 RGB 颜色 - 抱歉!
that was fun!
Examples:
You can check the source for all the methods, short version is add,remove,increase,decrease,more,less - full chaining on elements and colors, helper function Color() added to make things easier.
Hope that helps!
edit: just caught up on the thread (after doing this) and see you want 0xFFFF00 + 0x0000FF to make green, not white - sigh this won't do that, it just works with hex rgb colors - apologies!
这里的主要问题是理解加色/减色的概念:
蓝色 + 黄色 = 绿色仅适用于颜料(油漆、墨水等),因为
如果您使用灯光(<强>添加颜色)你会得到:
蓝色+黄色=白色
解决方案?如果你想描述减色(类似油漆的组合),你必须应用“补色之和”规则:(
事实上,我们得到一些深棕色,因为颜料从来都不是完美的)
那么为什么我们在现实生活中会变得绿色呢?因为我们不使用“蓝色”,而是使用青色(#00FFFF)并且:
我必须补充一点,这是描述颜色交互的一种非常简单的方法,但要复杂得多......
The main problem here is understanding the concept of additive/subtractive color :
blue + yellow = green only for pigments (paint, ink and so on) because they are subtractive color
if you're using lights (additive colors) you would get :
blue + yellow = white
The solution ? If you want to describe subtractive colors (paint-like combination) you have to apply the rule "sum the complementary colors" :
(in fact we get some dark brown because pigments are never perfect)
So why do we get green in actual life ? because we do not use "blue" but cyan(#00FFFF) and :
I have to add that this is a very simple way of describing the color interaction which is far more complicated...
我认为最接近的颜色模型是 CMY 颜色模型,幸运的是,它只是 RGB 的逆颜色模型。
现在,如果您假设(尽管 CMY 是青色-品红色-黄色)C = 蓝色,M = 红色,Y = 黄色,那么您就非常接近您的艺术颜色。例如:
更新:
0到1只是无色和全色的方便表示,相当于标准RGB的0到255。
对于那些想知道“这显然是错误的,青色怎么=蓝色?”的人,您还应该意识到CMY!=艺术颜色。事实上,艺术颜色与任何原色模型都不匹配,因此这只是一个合理的假设,可以通过在艺术中混合颜料来获得您期望获得的颜色。
I think the closest you can get is the CMY colour model which, fortunately, is just the inverse of RGB.
Now, if you assume (although CMY is cyan-magenta-yellow) that C = blue, M = red, Y = yellow, you get pretty close to your art colours. For example:
Update:
0 to 1 are just convenient representations for no colour and full colour, equivalent to 0 to 255 of standard RGB.
For those wondering "this is obviously wrong, how is cyan = blue?", you should also realize that CMY != art colour. In fact, art colour doesn't match any primary colour model, so this is just a reasonable assumption to get the kind of colours you expect to get by mixing paints in art.
阅读减色原色,然后您可以阅读RYB 颜色模型。
看来从 RYB 转换为 RGB 并不是那么容易,你可能需要检查这个计算器 (以及其背后的 JavaScript)。
Read about subtractive primaries, after which you can read on RYB color model.
It seems that converting from RYB to RGB is not so easy, you might want to check this calculator (and javascript behind it).
如果该方法需要更多解释或者您发现这里有问题,请告诉我!我必须说它工作得很漂亮!我还要指出,
less($color, $percent)
是相同的,只是减去了颜色。 Less 对我来说仍然没有直观意义(黄绿 - 绿色 = 棕色),但我很确定计算是正确的。再次感谢您的所有帮助!If the method needs more explanation or if you see something wrong here, let me know! I must say it works beautifully! I'll also note that
less($color, $percent)
is the same except you subtract the color instead. Less still doesn't make intuitive sense to me (yellow-green - green = brown), but I'm pretty sure the computation is correct. Thanks again for all your help!