十六进制网页颜色
您好,我在 php 中将颜色显示为十六进制值。是否可以通过从十六进制值中减去一个数字来改变颜色的深浅?我想要做的是显示生动的网络安全颜色,但如果选择的话,我想使颜色变暗或变浅。我知道我只能使用两种色调,但我可以使用数百种潜在的颜色。
需要明确的是,#66cc00 是亮绿色,#99ffcc 是非常淡的绿色。我要减去什么才能得到第二种颜色?有没有什么公式,因为我只能得到它。
感谢您的帮助
干杯
Hi I am displaying a colour as a hex value in php . Is it possible to vary the shade of colour by subtracting a number from the hex value ? What I want to do it display vivid web safe colour but if selected I want to dull or lighten the colour. I know I can just use two shades of colour but I could hundred of potential colours .
to be clear #66cc00 is bright green and #99ffcc is a very pale green . What do i subtract to get the second colour ? is there any formula because I just can get it .
Thanks for any help
Cheers
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
十六进制颜色由 6 个十六进制数字组成。前两位数字代表红色,后两位数字代表绿色,最后两位数字代表蓝色。在色调中,00 表示没有颜色,FF 表示颜色的最高值。因此,#FF0000 会是亮红色,没有绿色或蓝色,而 #00CCFF 会是非常蓝和一点绿色,根本没有红色。
您给出的示例颜色实际上具有不同的红色、绿色和蓝色组成。 #66CC00 大部分是绿色,带有一些红色,而 #99FFCC 大部分是绿色,带有一些蓝色和红色。
在将颜色转换为十进制之前,您应该将颜色分解为红色、绿色和蓝色分量,对两者进行平均,然后转换回来:
#66 CC 00 -> 102 204 0
#99 FF CC -> 153 255 204
两者之间的平均值:128 230 102:#80E666
找到中间颜色后,您可以近似最接近的网络安全颜色color: #99FF66
可以找到一个十六进制和十进制之间的转换器来自己执行此操作
这是一个 PHP 脚本,它可以满足您的需要。这是一个基于上述方法的 JavaScript 脚本(相关 十六进制JS 中的十进制转换):
Hex colors are made up of 6 hexadecimal digits. The first two digits are for the red shade, the second two are for the green, and the last two are for the blue. Within the shades, 00 is the absence of color and FF is the highest value for the color. So, #FF0000 would be bright red with no green or blue, and #00CCFF would be very blue and a little green with no red at all.
The example colors you give actually have a different makeup of red, green, and blue. #66CC00 is mostly green with some red while #99FFCC is mostly green with some blue and red.
You should break your colors into their red, green, and blue components before converting them to decimal, average the two, and then convert back:
#66 CC 00 -> 102 204 0
#99 FF CC -> 153 255 204
Average between the two: 128 230 102: #80E666
After finding the in-between color, you can approximate the closest web safe color: #99FF66
A converter between hexadecimal and decimal to do this yourself can be found here.
Here is a PHP script that does what you need. Here is a JavaScript script based off of the method described above (related hex to decimal conversion in JS):
为了进一步解释这一点:
以
#FFFFFF
为例,完全白色。它实际上由 3 个独立的颜色值红、绿和蓝组成。
FF(255,全红)、FF(255,全绿)、FF(255,全蓝)
如果您想要全红色,则可以使用
#FF0000
(255, 0, 0)颜色值从 0 到 255,您可以组合颜色值以获得最终颜色。
To explain this further:
Take
#FFFFFF
, completely white.It actually consists of 3 separate color values Red Green and Blue.
FF (255, full red), FF (255, full green), FF (255, full blue)
If you wanted a full red color you would instead use
#FF0000
(255, 0, 0)Color values goes from 0 to 255 and you combine colorvalues to get the final color.
它确实有助于理解颜色的情况,但如果您只是对颜色着色感兴趣,这是我在 php.net 上找到的内容。还没有尝试过,希望它能满足您的需求。
It really helps to understand what is going on with the colors, but if you're merely interested in shading a color, here's what I found on php.net. Haven't tried it (yet) tough, hope it suits your needs.
这称为十六进制基数:
1 2 3 4 5 6 7 8 9 ABCDEF
“E”在十六进制(16)基数中是“14”在十进制(10)基数中
66cc00(16) == 6736896(10)
99ffcc(16) = = 10092492(10)
This is called hexadecimal base:
1 2 3 4 5 6 7 8 9 A B C D E F
"E" in hexadecimal (16) base is "14" in decimal (10) base
66cc00(16) == 6736896(10)
99ffcc(16) == 10092492(10)
网页颜色由红色、绿色和蓝色值指定。每个组件的值从
00
(0) 到FF
(255)。组件越多,该组件就会显得越亮。如果所有三个组件都相同,您会得到从黑色 (#000000
) 到白色 (#ffffff
) 的灰色阴影。所以在你的第一个例子中,有一些红色和很多绿色。在第二个颜色中,每种颜色都有更多的颜色,这使得整个颜色更浅。绿色仍然是主要成分,但这就是为什么它是浅色的。
要从一种颜色更改为另一种颜色,您需要改变每个分量的值,但这并不总是像从一个值减去另一个值那么简单。
从第一种颜色到第二种颜色,您想要实现什么目标?
Web colours are specified by their red, green and blue values. Each component being a value from
00
(0) toFF
(255). The more of a component there is the brighter that component will appear. If all three components are the same you get shades of grey from black (#000000
) to white (#ffffff
).So in your first example there's some red and a lot of green. In your second there's more of every colour which makes the whole colour lighter. Green is still the main component though which is why it's a pale colour.
To change from one colour to another you need to vary the values of each component, but it's not always as simple as subtracting one value from another.
What are you trying to achieve to get from your first to your second colour?
网页颜色为 RGB。要影响颜色的饱和度和值而不影响色调,必须转换为 HSV 颜色空间。
PHP 中的 RGB 到 HSV
获得 HSV 后,更改 V 分量以制作颜色变暗或变亮并更改 S 分量以使颜色或多或少饱和或苍白。调整 S 和 V 后转换回 RGB。为了确保颜色网络安全,请将每个 RGB 通道舍入为 51 或 0x33 的倍数。
Web colors are in RGB. To affect the saturation and value of the color without affecting the hue, you must convert to HSV color space.
RGB to HSV in PHP
Once you have HSV, change the V component to make a color darker or lighter and change the S component to make the color more or less saturated or pale. After tweaking the S and V convert back to RGB. To make the colors web safe, round each RGB channel to a multiple of 51 or 0x33.