CCParticleExplosion color? - colors

I realize that a ccColor3B takes RGB values between 0 and 255, but in a ccColor4F, the values are between 0 and 1.0? I have custom colors I would like to use something like ccColor3B blueColor = ccc3(61, 66, 255); as a ccColor4F. I've tried dividing by 255 but all of the colors show up as black for some reason. My code looks something like: ccColor4F startColor;
startColor.r = blueColor/255;
startColor.g = blueColor/255;
startColor.b = blueColor/255;
startColor.a = 1.0f;
I'm not sure what I'm doing wrong!

You need to deal with each RGB value individually. For example:
startColor.r = blueColor.r / 255.0f;
Or you could just use what's already in place to do that conversion.
From ccTypes.h:
static inline ccColor4F ccc4FFromccc3B(ccColor3B c)

Related

How to get white color in PyQT

the following instructions gets me rgb value in pyqt
c = result.pixel(x,y)
r = str(QtGui.qRed(c))
g = str(QtGui.qGreen(c))
b = str(QtGui.qBlue(c))
is there any way to get white value ?
for example: QtGui.qWhite(c) something ??
QtGui.QColor.fromRgb(c).lightness()
should do.
If you need to extract from a QColor not just the light but also the hue and saturation you can use getHsl:
>>> QColor(0,128,80).getHsl()
(157, 255, 64, 255)

How to store RGB colour in variable?

I'm looking to store an RGB colour in a variable in an Excel VBA project, to set the background color of various cell/ranges throughout a sub.
I want to set the colour once in a variable, so if I decide to change it throughout I only need to do it in one place.
Dim clrBlue As ColorFormat
clrBlue = RGB(0, 0, 256)
Range("a2").Interior.Color = clrBlue
Range("b3").Interior.Color = clrBlue
With the above code, I'm getting runtime error:
Object variable or With block variable not set
I could write separate functions (SetBlue, SetRed, SetGreen) to apply each colour, but that feels messy.
Can anyone suggest what I'm doing wrong?
RGB returns a Long, so you need to declare clrBlue as Long instead of as ColorFormat.
Dim clrBlue As Long
clrBlue = RGB(0, 0, 255)
Application.union(Range("A2"), Range("B3")).Interior.Color = clrBlue
As others have said, RGB() returns a Long, so you'll need to use that instead of ColorFormat. On a somewhat related note, I really like the Color enum in C#, and I started mimicking that in my VBA modules. You can create your own enum to store the values of colors in your project, then reference the color with Color.Blue.
This also makes it really easy to modify a color, if you decide to go with a different shade of blue. Update the enum, and all of the places you've used Color.Blue will update.
Example:
Public Enum Color
Black = 0 'RGB(0, 0, 0)
Blue = 14390640 'RGB(112, 149, 219)
Gray = 11842740 'RGB(180, 180, 180)
Red = 6118894 'RGB(238, 93, 93)
White = 16777215 'RGB(255, 255, 255)
End Enum
To get the long value of the RGB value to store, I just threw the value into the Immediate window and copied the output.
In Immediate Window, type:
? RGB(112, 149, 219)
The output will be 14390640. There might be an easier way to get the value.
I haven't tried this and I'm not disputing any of the previous commenters.
I do notice that the original code sample has: clrBlue = RGB(0, 0, 256)
The highest number allowed in RGB is 255. That might be the problem.

d3 color scale - linear with multiple colors?

I'm trying to create something a little like a quantize scale, but act like a linear color scale?
When I try to put multiple colors into a linear scale, it seems to only scale between the first two colors.
I'd like multiple colors like a quantize scale, but fade between those colors. I'm unsure if this is possible.
//red and green works ok
var color = d3.scale.linear()
.range(['red','green']);
//doesn't work - only red and green show
var color = d3.scale.linear()
.range(['red','green', 'blue']);
//red green and blue show, however it doesn't fade between the colors
var color = d3.scale.quantize()
.range(['red','green', 'blue']);
You have to use domain 'pivot' value like:
d3.scale.linear()
.domain([0, pivot, max])
.range(['red', 'green', 'blue']);
From the documentation for continuous scale domains:
Although continuous scales typically have two values each in their domain and range, specifying more than two values produces a piecewise scale. For example, to create a diverging color scale that interpolates between white and red for negative values, and white and green for positive values, say:
var color = d3.scaleLinear()
.domain([-1, 0, 1])
.range(["red", "white", "green"]);
color(-0.5); // "rgb(255, 128, 128)"
color(+0.5); // "rgb(128, 192, 128)"
Anto's solution works great if you want to blend 3 colors. In my case, I needed a way to blend an arbitrary number of colors. For me, the trick was to set up the domain correctly. Take for example, the following array of colors:
var colors = ['#084594', '#2171b5', '#4292c6', '#6baed6', '#9ecae1', '#c6dbef', '#eff3ff'];
You can create a domain array with values from -1 to +1 like this:
var domain = [-1];
var increment = 2/(colors.length-1);
for (var i=0; i<colors.length-2; i++){
var previous = domain[domain.length-1];
domain.push(previous+increment);
}
domain.push(1);
Once the domain array is created, you can create a color function like this:
var getColor = d3.scaleLinear()
.domain(domain)
.range(colors);
If you want to get color values at specific percentages (like chroma does), you can do something like this:
var p = 0.25; //Valid range for p is 0.0-1.0
var x = (p*2)-1;
var color = d3.color(getColor(x));
console.log(color.formatHex());

How to alter brightness of a single rgb color simply and easily via php?

A quesion about RGB color and finding the simplest, tiniest, php conversion code for manipulating the lightness/darkness of a given RGB hue.
Imagine a variable $colorA containning a valid six char RGB color, like F7A100 which we want to make a bit lighter and/or darker:
$color = B1B100; // original RGB color manually set.
Then, at any page have that color bit darker/lighter on the fly:
$colorX = someFunction($color, +10); // original color 10 steps lighter
$colorY = someFunction($color, -25); // original color 25 steps darker
What would be YOUR way of solving this? Keep the RGB as is or first change it to HSL? Hints and suggestions are welcome. Your sample/code is welcome too.
This really focuses to the TINIES / SIMPLES / SHORTEST possible code to just make the same hue bit darker/lighter.
I deliberately do not suggest my code, as I want to keep possibilities open in here.
The absolutely simplest solution is to add some constant (like 1) to each part of the color representation: [R, G, B]. This is due to the fact that max values of all [R, G, B] represent white, while min values - black. In pseudo-code (assuming 255 is max, sorry, I don't know PHP):
lighter(R, G, B) = [
min(255, R + 1),
min(255, G + 1),
min(255, B + 1)
]
You must keep in mind though that this transformation is way too simplistic and the proper implementation would be to convert to HSL/HSB, increase H and transform back to RGB.
For slight alteration of brightness you can convert the hexadecimal values to decimal, manipulate them and convert back to hexadecimal like this:
function alterBrightness($color, $amount) {
$rgb = hexdec($color); // convert color to decimal value
//extract color values:
$red = $rgb >> 16;
$green = ($rgb >> 8) & 0xFF;
$blue = $rgb & 0xFF;
//manipulate and convert back to hexadecimal
return dechex(($red + $amount) << 16 | ($green + $amount) << 8 | ($blue + $amount));
}
echo alterColor('eeeeee', -10); //outputs e4e4e4
Beware that this code does not handle overflow for one color - if one color value becomes less than 0 or more than 255 you will get an invalid color value. This should be easy enough to add.
For drastic changes in brightness, convert to HSL and manipulate the lightness.
Using the functions from the Drupal code, this can be done like this:
$hsl = _color_rgb2hsl(_color_unpack('eeeeee'));
$hsl[2] -= 10;
$rgb = _color_pack(_color_hsl2rgb($hsl));
echo $rgb; //outputs e4e4e4

How to generate a set of random colors where no two colors are almost similar?

I currently use the following function to generate a random hexadecimal representation of a color.
function getRandomColor($max_r = 192, $max_g = 192, $max_b = 192) {
if ($max_r > 192) { $max_r = 192; }
if ($max_g > 192) { $max_g = 192; }
if ($max_b > 192) { $max_b = 192; }
if ($max_r < 0) { $max_r = 0; }
if ($max_g < 0) { $max_g = 0; }
if ($max_b < 0) { $max_b = 0; }
return '#' . dechex(rand(0, 192)) . dechex(rand(0, 192)) . dechex(rand(0, 192));
}
Notice that I set the max value to be 192 instead of 255 for the sole reason that I am avoiding very light colors, for the purpose that I would be using the random color as foreground in a white background.
My question is how do I generate an indefinitely numbered set of colors where there are no colors that are almost the same. e.g.: #D964D9 & #FF3EFF ?
It might be better to use HSV coordinates. If you don't need white or black, you can set S and V to their maximum values, and generate H values that are not too close to each other (mod 360 degrees). Then convert to RGB.
There are several methods which spring to mind:
Set up a array of n standard colors and interchange them randomly to produce the desired "random" colors.
Fill an array of n colors; generate a random color and check if there is something "close" already in the array. If so, choose another random color.
Select each color as a deterministic sequence, like a simple hash value, designed to not produce duplicate values. Grey code springs to mind.
Your algorithm could randomly generate RGB colors (as it's doing now) however you could for example verify that the two R's are sufficiently different before accepting the color choice. The algorithm could repeat that step (say up to 4...10...N times) for a given R, G and/or B.
while ( (R1 > $max_r/2) && (R2 > $max_r/2) ) {
// Both are in the upper half of range, get a new random value for R1.
}
Other possibilities:
Repeat for the lower half of range
Further sub-divide ranges (into 1/3's or 1/4's)
Repeat for G and B tones

Resources