最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - Programmatic gradient stops with Javascript - Stack Overflow

programmeradmin1浏览0评论

Working with Javascript (jQuery), given 2 color values (2033ff and 3300a0 for example) how can I determine certain gradient stops between them?

Reason being is, I intend on using an array of color values:

    0 => '000000'
 8400 => 'f0ff00'
44000 => '2033ff'
68400 => '3300a0'

There being 86400 seconds in a day, 12:00AM maps to 0, and 11:59PM maps to 86399. As time passes, the background color of a specified element changes to the appropriate color in gradient list via window.setInterval(function(e){ ... }, 1000). For example 2:32:11PM = 52331, which from the example would be somewhere between 2033ff and 3300a0.

I don't need to populate the array with the values (unless that would be easier) but instead use the index and value as references.

Working with Javascript (jQuery), given 2 color values (2033ff and 3300a0 for example) how can I determine certain gradient stops between them?

Reason being is, I intend on using an array of color values:

    0 => '000000'
 8400 => 'f0ff00'
44000 => '2033ff'
68400 => '3300a0'

There being 86400 seconds in a day, 12:00AM maps to 0, and 11:59PM maps to 86399. As time passes, the background color of a specified element changes to the appropriate color in gradient list via window.setInterval(function(e){ ... }, 1000). For example 2:32:11PM = 52331, which from the example would be somewhere between 2033ff and 3300a0.

I don't need to populate the array with the values (unless that would be easier) but instead use the index and value as references.

Share Improve this question edited Jan 2, 2011 at 19:48 Dan Lugg asked Jan 2, 2011 at 19:34 Dan LuggDan Lugg 20.6k19 gold badges115 silver badges179 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

Just do a linear interpolation:

Given 2033ff and 3300a0 as start and end you'll do:

red1 = 0x2033ff >> 16;
green1 = (0x2033ff >> 8) & 0xFF;
blue1  = 0x2033ff & 0xFF;

red2 = 0x3300a0 >> 16;
green2 = (0x3300a0 >> 8) & 0xFF;
blue2  = 0x3300a0 & 0xFF;

time = 0.3 // This should be between 0 and 1

outred = time * red1 + (1-time) * red2;
outgreen = time * green1 + (1-time) * green2;
outblue = time * blue1 + (1-time) * blue2;

I wrote the library RainbowVis-JS as a general solution for mapping numbers to colours. For your example, you would use:

var rainbow = new Rainbow(); 
rainbow.setSpectrum('2033ff', '3300a0');
rainbow.setNumberRange(1, 86400);
var colour = rainbow.colourAt(52331); // 2c14c5
发布评论

评论列表(0)

  1. 暂无评论