Changing Color Incrementally

Changing color incrementally

Since you haven't said that you want a CSS only solution, I would recommend you to use a SASS/LESS based solution.

http://sassmeister.com/gist/925757ff999824ec0270

$baseColor: #FFCC00;

@for $i from 1 to 5 {
#mainbody :nth-child(#{$i}) {
border-color: adjust-color($baseColor, $green: ($i - 1) * 1);
}
$i: $i + 1;
}

The above code generates this:

#mainbody :nth-child(1) {
border-color: #ffcc00;
}

#mainbody :nth-child(2) {
border-color: #ffcd00;
}

#mainbody :nth-child(3) {
border-color: #ffce00;
}

#mainbody :nth-child(4) {
border-color: #ffcf00;
}

Incrementally change the color of text using CSS?

Not 100% CSS solution (99%), but you can achieve something like that using transitions. The only "non-CSS" part is dynamically changing a class. But that's the closest you can get.

CSS

p { color: #ccc }
p.red {
color: #c00;
transition: all 10s linear; // <-- this is the key
}

JS

document.getElementsByTagName('p')[0].classList.add('red');

See here: http://jsfiddle.net/vek4d/

So basically, it sets a 10-second animation in motion as soon as the class changes.


So, if you really want to go crazy with that, I have another idea.

You can do masking - simply duplicate your paragraph and put a fully-red and transparent copy on top of it. Then, to increase the redness, just tweak the opacity of the mask element. Here's an example (note I'm using 75% instead of 15% to make it more obvious): http://jsfiddle.net/vek4d/1/

Increment matplotlib color cycle

You could call

ax2._get_lines.get_next_color()

to advance the color cycler on color. Unfortunately, this accesses the private attribute ._get_lines, so this is not part of the official public API and not guaranteed to work in future versions of matplotlib.

A safer but less direct way of advance the color cycler would be to plot a null plot:

ax2.plot([], [])

import numpy as np
import matplotlib.pyplot as plt

x = np.arange(10)
y1 = np.random.randint(10, size=10)
y2 = np.random.randint(10, size=10)*100
fig, ax = plt.subplots()
ax.plot(x, y1, label='first')
ax2 = ax.twinx()
ax2._get_lines.get_next_color()
# ax2.plot([], [])
ax2.plot(x,y2, label='second')

handles1, labels1 = ax.get_legend_handles_labels()
handles2, labels2 = ax2.get_legend_handles_labels()
ax.legend(handles1+handles2, labels1+labels2, loc='best')

plt.show()

Sample Image



Related Topics



Leave a reply



Submit