What's this numeric notation called? - colors

add(new CustomLabelField
("Please enter your credentials:", Color.WHITE, 0x999966, 0));
What is that 0x999966?
I want to place this color there so how can I convert it? I just need an online tool that will convert it for me but I don't even know what it's called! :D
link for more info
Thanks!
Edit: OK, so this is called a hexadecimal number, but I still don't know how to convert something like "#716eb3" to the notation accepted by the CustomLabelField constructor. Any help?

Hexadecimal, as in, base 16. If 0x999966 is indicating a color, then it's usually encoded such that each two hex digits encode a color (as RGB), so it's 0xRRGGBB.

Thats a number in base 16 or hexadecimal number.

Looks like a hexadecimal representation of a RGB. For example with #716eb3:
add(new CustomLabelField
("Please enter your credentials:", Color.WHITE, 0x716eb3, 0));

It looks like it might be in Java.
First, take the substring to remove the # prefix, then use Integer.parseInt("9999966", 16): that's parseInt with the radix for the base (see Integer documentation). Something along these lines:
String colour = "#716eb3";
colour = colour.substring(1,colour.length()-1);
add(new CustomLabelField ("Please enter your credentials:",
Color.WHITE, Integer.parseInt(colour,16), 0));

In many programming languages, numbers prefixed with 0 are octal and with 0x are hexadecimal.
Search for Hexadecimal to Decimal or Hexadecimal to RGB converters.

If it's C#, the format expected by the CustomLabelField constructor is most likely System.Drawing.Color that uses the ARGB format.
“The color of each pixel is represented as a 32-bit number: 8 bits each for alpha, red, green, and blue (ARGB). The alpha component specifies the transparency of the color: 0 is fully transparent, and 255 is fully opaque. Likewise, an A value of 255 represents an opaque color. An A value from 1 through 254 represents a semitransparent color. The color becomes more opaque as A approaches 255.”
Converting the ARGB format into a System.Drawing.Color can be done as follows:
Color myColor = ColorTranslator.FromHtml("#FF999966");

Related

Web Assembly drawing gray canvas

I'm using Go and compiling it to web assembly.
I'm trying to render a bunch of rectangles next to eachother with a random colour, but they keep rendering as just gray.
My render function looks something like this:
for row,_ := range rows {
for col,_ := range row {
ctx.Set("fillStyle", fmt.Sprintf("#%06x", rand.Int()))
ctx.Call("fillRect", 20, 20 + (col * width), maxHeight - (row*height))
}
}
With which it renders a big block (all rectangles are next to eachother) but just all in gray, instead of doing them in different colours.
Is this enough code in the example to help further? If not I can post it to a gist, as I'm new to WASM I'm unsure which parts could really be relevant - but those 2 functions are the only ones doing something with rendering as far as I can tell.
The problem is that you use this expression to construct the fill style:
fmt.Sprintf("#%06x", rand.Int())
rand.Int() returns a non-negative pseudo-random int. Size of int is 64 bits if GOOS=js and GOARCH=wasm. What this means is that the random int number will be random 8 bytes (first bit being always 0 due to being non-negative).
If you format such a number with the %06x verb, like almost all the time it will be more than just 6 hex digits. The width 6 means to be at least 6, and the flag 0 means to pad with zeros if less. But if it's longer, it is not truncated.
And if you set an invalid color to canvas.fillStyle, it will disregard it and the last set valid fill style will remain active. And I'm guessing it was a gray color you used before the loop.
Fix is easy, just make sure the random number has no more than 3 bytes, or in other words, 6 hex digits. Use a simple bitmask:
ctx.Set("fillStyle", fmt.Sprintf("#%06x", rand.Int()&0xffffff))
Or use rand.Intn() instead of rand.Int():
ctx.Set("fillStyle", fmt.Sprintf("#%06x", rand.Int(0x1000000)))
Also context.fillRect() expects 4 arguments: x, y, width and height, so it should be something like this:
ctx.Call("fillRect", 20+(col*width), maxHeight-(row*height), width, height)

Setting color variable

I need to compare colors. I want to set a color into a variable and then compare that it a value obtained using getPixel.
However, the following does not work. It seems like ImageJ does not know that the value in basecolor is a color.
basecolor = 0xFFFFFF;
rightpixel = getPixel(x, y);
if (rightpixel == basecolor) count++;
Your problem is in getPixel which does not yield a color written in hex.
I present to you your best friend on ImageJ : the built-in macro functions code
https://imagej.nih.gov/ij/developer/macro/functions.html
which documents built-in pixel functions such as getPixel().
For getPixel(), it is stated "Note that pixels in RGB images contain red, green and blue components that need to be extracted using shifting and masking. See the Color Picker Tool macro for an example that shows how to do this. ", and the Color Picker Tool macro tells us how to go from color "bits" to RGB.
So if you wish to compare colors, you do :
basecolor=newArray(0,0,0);
rightpixel = getPixel(x,y);
//from the Color Picker Tool macro
//converts what getPixel returns into RGB (values red, green and blue)
if (bitDepth==24) {
red = (v>>16)&0xff; // extract red byte (bits 23-17)
green = (v>>8)&0xff; // extract green byte (bits 15-8)
blue = v&0xff; // extract blue byte (bits 7-0)
}
//compare the color with your color
if(red==basecolor[0] && green==basecolor[1] && blue==basecolor[2]){
print("Same Color");
count++;
}
//you can also work with hex by converting the rgb to hex and then
//comparing the strings like you did

What is the CieLuv value of Black?

The Wikipedia page about CieL*u*v* color space describes the conversion function from a color expressed in the XYZ color space.
For black, expressed as (R=0, G=0, B=0) in the RGB color space, and (X=0, Y=0, Z=0) in the XYZ color space, which values should we use to represent it in CieLuv?
The formula uses a division, 4x/(x+15y+3z), whose result is undefined for black.
Should we use (L=0, u=0, v=0), or are there more subtleties?
In fact black is not represented in uv.
So in Luv black can be represented by L = 0 and u = v = undefined.
Well there's a difference between theoretical black" (all zeros) and "practical black" which is 1/1000 the lightness of white.
See "Charles Poynton. Digital Video and HD. Morgan Kaufmann, second
edition edition, 2012.", page 248.
So in practice it's not exactly zero.
Or maybe imagine just grays: They all have u* = v* = 0, only L* is different.
So when you approach black, it should be clear what it looks like.

How to invert an RGB color in integer form?

Given an RGB color in 32-bit unsigned integer form (eg. 0xFF00FF), how would you invert it (get a negative color), without extracting its individual components using bitshift operations?
I wonder whether it's possible using just bitwise operations (AND, OR, XOR).
More precisely, what's the algorithm that uses the least number of instructions?
I think it is so simple.
You can just calculate 0xFFFFFF-YourColor. It will be the inverted color.
int neg = 0xFFFFFF - originalRGB
// optional: set alpha to 255:
int neg = (0xFFFFFF - originalRGB) | 0xFF000000;
Use this method to invert each color and maintain original alpha.
int invert(int color) {
return color ^ 0x00ffffff;
}
xor (^) with 0 returns the original value unmodified.
xor with 0xff flips the bits. so in the above case we have 0xaarrggbb we are flipping/inverting r, g and b.
This should be the most efficient way to invert a color. arithmetic is (marginally) slower than this utterly simple bit-wise manipulation.
if you want to ignore original alpha, and just make it opaque, you can overwrite the alpha:
int invert(int color) {
0xff000000 | ~color;
}
in this case we just flip every bit of color to inverse every channel including alpha, and then overwrite the alpha channel to opaque by forcing the first 8 bits high with 0xff000000.
You could simply perform the negation of the color. Snippet:
~ color
Your question is unclear; no colors in RGB are "negative colors".
You could invert an image, as though it was a film negative. Is that what you meant?
If you wanted to invert an image that has just one pixel of color 0xFF00FF, the calculation is to subtract from white, 0xFFFFFF.
> negative_result_color = 0xFFFFFF - 0xFF00FF
> negative_result_color == 0x00FF00
true
In a computer, a subtraction is done by adding the compliment:
http://en.wikipedia.org/wiki/Method_of_complements#Binary_example
But seriously, why wouldn't you just let the machine do the subtraction for you with your ordinary code? Its what they're good at.
Color color_original = Color.lightGray;
int rgb = color_original.getRGB();
int inverted = (0x00FFFFFF - (rgb | 0xFF000000)) | (rgb & 0xFF000000);
Color color_inverted = new Color(inverted);

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

Resources