JavaScript 颜色渐变
使用带或不带 Jquery 的 javascript,我需要根据开始和结束颜色创建颜色渐变。这可以通过编程来完成吗?
结束颜色只会是开始颜色的较暗阴影,并且它用于无序列表,我无法控制 li 项目的数量。我正在寻找一种解决方案,允许我选择开始和结束颜色,将十六进制值转换为 RGB,以便可以在代码中对其进行操作。起始 RGB 值按根据项目数量计算的步长值递增。
因此,如果列表有 8 个项目,则需要分 8 步递增单独的红绿蓝值才能获得最终颜色。有没有更好的方法来做到这一点,如果有的话,我在哪里可以找到一些示例代码?
Using javascript with or without Jquery, I need to a create a gradient of colours based on a start and finish color. Is this possible to do programmatically?
The end colour is only ever going to be darker shade of the start colour and it's for an unordered list which I have no control over the number of li items. I'm looking for a solution that allows me to pick a start and end color, convert the hex value into RGB so it can be manipulated in code. The starting RGB values gets incremented by a step value calculated based upon the number of items.
so if the list had 8 items then the it needs to increment the seperate Red Green Blue values in 8 steps to achieve the final colour. Is there a better way to do it and if so where can I find some sample code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(15)
我创建了一个 JS 库 RainbowVis-JS 来解决这个普遍问题。您只需使用
setNumberRange
设置项目数量,并使用setSpectrum
设置开始和结束颜色。然后您可以使用colourAt
获取十六进制颜色代码。欢迎您查看该库的源代码。 :)
I created a JS library, RainbowVis-JS to solve this general problem. You just have to set the number of items using
setNumberRange
and set the start and end colour usingsetSpectrum
. Then you get the hex colour code withcolourAt
.You are welcome to look at the library's source code. :)
这是一个生成颜色数组的函数!
Here is a function to generate an array of colors!
是的,绝对是。
我在 Java 中完成此操作,在 JavaScript 中也应该相当简单。
首先,您需要将颜色分解为 RGB 分量。
然后计算组件的开始和结束之间的差异。
最后,计算百分比差异并乘以每个分量的起始颜色,然后将其添加到起始颜色。
假设您可以获得 RGB 值,这应该可以做到:
“percentFade”是一个浮动小数,表示淡入“endColor”的程度。 1 将是完全淡入淡出(从而创建最终颜色)。 0 表示不褪色(起始颜色)。
Yes, absolutely.
I do this in Java, should be fairly simple to do in JavaScript too.
First, you'll need to break the colors up into RGB components.
Then calculate the differences between start and finish of the components.
Finally, calculate percentage difference and multiply by the start color of each component, then add it to the start color.
Assuming you can get the RGB values, this should do it:
The "percentFade" is a floating decimal, signifying how far to fade into the "endColor". 1 would be a full fade (thus creating the end color). 0 would be no fade (the starting color).
我根据@desau 的回答使用这个函数:
I use this function based on @desau answer:
desau 的回答很棒。这是 JavaScript 中的:
desau's answer is great. Here it is in javascript:
有一个 JavaScript 库可以创建颜色渐变:
javascript-color-gradient
< a href="https://i.sstatic.net/LayRx.png" rel="noreferrer">
There is a JavaScript library which can create color gradients:
javascript-color-gradient
基于 @drinor 的回答 - TypeScript 支持
示例:
=> “#996600”
Based on @drinor's answer - TypeScript support
example:
=> "#996600"
chroma.js:
chroma.js:
xolor 库具有渐变功能。这将创建一个具有从起始颜色到结束颜色渐变的 8 种颜色的数组:
请在 github 上查看更多信息:https ://github.com/fresheneesz/xolor
The xolor library has a gradient function. This will create an array with 8 colors in a gradient from a start color to an end color:
See more on github: https://github.com/fresheneesz/xolor
不是那么强大,但在大多数情况下都可以工作,并且您不必为以下代码包含除 jQuery 之外的任何其他库:
HTML:
JavaScript:
这应该创建一个包含彩虹的 div。请参阅http://jsfiddle.net/rootandy/54rV7/
Not such mighty but in most cases working and you do not have to include any other libraries except jQuery for the following code:
HTML:
JavaScript:
This should make an div that contains a rainbow. See http://jsfiddle.net/rootandy/54rV7/
我需要为一组未知的动态元素创建足够大的颜色选项数组,但我需要每个元素通过开始颜色和结束颜色递增它们的方式。这种方法遵循“百分比褪色”方法,只是我很难遵循这种逻辑。这就是我使用两个 RGB 颜色值的输入并计算页面上元素数量的方法。
这里有一个 codepen 链接,演示了这个概念。
下面是问题的代码片段。
I needed to create a large enough array of color options for an unknown set of dynamic elements, but I needed each element to increment their way through a beginning color and an ending color. This sort of follows the "percent fade" approach except I had a difficult time following that logic. This is how I approached it using inputs of two rgb color values and calculating the number of elements on the page.
Here is a link to a codepen that demonstrates the concept.
Below is a code snippet of the problem.
您可以检索元素列表。我不熟悉 jQuery,但prototypejs 有 Element.childElements() 它将返回一个数组。一旦知道了数组的长度,您就可以确定每一步要改变多少像素分量。以下一些代码我尚未以我所呈现的形式进行测试,但希望它能给您一个想法。
You can retrieve the list of elements. I'm not familiar with jQuery, but prototypejs has Element.childElements() which will return an array. Once you know the length of the array, you can determine how much to change the pixel components for each step. Some of the following code I haven't tested out in the form I'm presenting it in, but it should hopefully give you an idea.
基本 Javascript - 背景渐变
这里有一个现成的函数,可以
使用 CSS
将元素背景设置为渐变和用法:
更新其他浏览器
这是使用 chrome 进行测试的,我将尝试使用 Canvas
最基本的水平是:
和用法:
小提琴:
https://jsfiddle.net/jch39bey/
-
添加垂直渐变
一个简单的标志来设置垂直
和用法:
Basic Javascript - Background Gradient
Here's a ready-made function to set an elements background to be a gradient
Using CSS
And Usage :
This was tested working with chrome, I'll try to update for other browsers
Using Canvas
The most basic horizontal would be :
And Usage :
A Fiddle :
https://jsfiddle.net/jch39bey/
-
Adding Vertical Gradient
A simple flag to set vertical
And Usage :
基于 @desau 的回答和其他地方的一些代码,这里有一个 jQuery 分步演练:
共有三个函数:
coloursBetween(fromColour, toColour, numberOfColours)
getRGBAValues(string)
code>hexToRGBA(hex, alpha)
调用主函数
coloursBetween()
传入起始颜色和结束颜色,以及你想要的颜色总数已经回来了。因此,如果您请求返回十种颜色,您将获得第一个颜色 + 8 个渐变颜色 + 最后一个颜色。colorsBetween 函数首先将传入的十六进制颜色(例如#FFFFFF、#000000)转换为 rgba(例如 rgba(255,255,255,1) rgba(0,0,0,1)),然后从中减去红色、绿色和蓝色值每个。
然后计算红色、绿色和蓝色之间的差异。在此示例中,每种情况均为 -255。计算增量并将其用于乘以红色、绿色和蓝色的新增量值。 Alpha 始终假定为 1(完全不透明)。然后将新值添加到颜色数组中,并在 for 循环完成后将其返回。
最后,像这样调用(从红色到蓝色):
您可以使用它来执行以下操作:
Based on @desau's answer and some code from elsewhere, here's a jQuery step-by-step walkthrough:
There are three functions:
coloursBetween(fromColour, toColour, numberOfColours)
getRGBAValues(string)
hexToRGBA(hex, alpha)
Call the main function
coloursBetween()
passing in the starting colour and the ending colour, as well as the total number of colours you want to have returned. So if you request ten colours returned, you get the first from colour + 8 gradient colours + the final to colour.The coloursBetween function starts by converting the incoming hex colours (e.g. #FFFFFF, #000000) into rgba (e.g. rgba(255,255,255,1) rgba(0,0,0,1)) and then subtracting the Red, Green and Blue values from each.
The difference between the Reds, Greens and Blues is then calculated. In this example it's -255 in each case. An increment is calculated and used to multiply new incremental values for the Red, Green and Blue. Alpha is always assumed to be one (full opacity). The new value is then added to the colours array and after the for loop has completed, it's returned.
Finally, call like this (going from Red to Blue):
which you can use to for something like this:
这是一个可以满足您要求的脚本:
https://gist.github.com/av01d/538b3fffc78fdc273894d173a83c563f
非常易于使用:
Here's a script that does just what you're asking for:
https://gist.github.com/av01d/538b3fffc78fdc273894d173a83c563f
Very easy to use: