get the same color but then with a alpha value
in
Programming Questions
•
1 year ago
Let's say i have a white background and i want 10 rows on it with a very light blue color.
Every row color should look close to the other rows except that they have a different alpha value.
So 1 row has just a very light blue color with no alpha, the other one has a dark blue color with alpha and there for look the same as the other one).
(it will be more problematic with darker colors then with light colors).
I thought this was helpfull but i'm not sure anymore:
Cause this is the resulting thing:
- int rows = 10;
- void setup() {
- size(400, 400);
- background(255);
- color c = color(200, 100, 0, 255);
- float rowWidth = width / (float)rows;
- for (int i = 0; i < rows; i++) {
- float xPos = i*rowWidth;
- float alpha = norm(i, 0, rows-1);
- fill(getAlphaColor(c, alpha));
- rect(xPos, 0, rowWidth, height);
- }
- }
- color getAlphaColor(color c, float alpha) {
- int r = (c >> 16) & 0xFF; // Faster way of getting red(argb)
- int g = (c >> 8) & 0xFF; // Faster way of getting green(argb)
- int b = c & 0xFF; // Faster way of getting blue(argb)
- float newR = (r - (1-alpha) * 255) / alpha;
- float newG = (g - (1-alpha) * 255) / alpha;
- float newB = (b - (1-alpha) * 255) / alpha;
- println(newR+"\t"+newG+"\t"+newB+"\t"+alpha*255);
- return color(newR, newG, newB, alpha*255);
- }
1