I have stored procedure running in T-SQL 2012 and got filed to generate random Hex colours which is all working fine except generated Hex code are in dark colour combination like dark blue, dark green etc. I need to generate light colour scheme, how can I do that.
SELECT
RecordId,
CONVERT(VARCHAR(max), CRYPT_GEN_RANDOM(3), 2)as [BarColour]
The method you use will produce light colors as well as dark colors. If you're only getting dark colors, it's just bad luck.
A starting point for generating only light colors would be to do something like this. It randomly generates the RGB values and concatenates them into your hex codes.
SELECT CONCAT(RIGHT(CONVERT(VARCHAR(100),CAST((ABS(CHECKSUM(NEWID()) % 135) + 120) AS VARBINARY),2),2),RIGHT(CONVERT(VARCHAR(100),CAST((ABS(CHECKSUM(NEWID()) % 135) + 120) AS VARBINARY),2),2),RIGHT(CONVERT(VARCHAR(100),CAST((ABS(CHECKSUM(NEWID()) % 135) + 120) AS VARBINARY),2),2))
I ran this repeatedly to output results 10 at a time and I got back a range of light grey-hues and pastels. This method won't give you any pure colors though as it completely avoids the low range values.
Related
AutoHotkey's PixelSearch allows one to search for a pixel of a certain color in an (X1,Y1)..(X2,Y2) rectangle.
But I want to find any pixel that isn't a certain color, i.e. given a background color, I want to detect when any other color comes up in the foreground.
Is there a way to do this using the built-in PixelSearch, or other built-in functions?
If you have some control over the background color, perhaps you can use PixelSearch's variation parameter.
If your background color is black, search for a white pixel with variation 254. PixelSearch should return any pixel that isn't black.
This code searches for any pixel other than BLACK (untested)
White := 0xFFFFFF
PixelSearch OutputVarX, OutputVarY, X1, Y1, X2, Y2, %White%, 254
If your background is midtone gray (0x7F7F7F), try searching for black and white with variation 127. Again, I haven't tested this. Maybe the variation should be 126 or 63. You might have to engage in some trial and error since the behavior of the variation parameter isn't comprehensively documented.
You can use PixelGetColor to look at an individual pixel. The instruction tells you the color of the pixel at location x, y. An If command determines whether the pixel is 0x9D6346 or not.
Using PixelGetColor in a Loop allows you to test a range of pixels, one at a time by incrementing x, y or both.
And yes the proper order is BGR not RGB. A look at the help file confirmed that. :)
Something is happening to change the color. Perhaps you can look for another way to detect the change on the screen. Is the change of color due to another window or message becoming active? If so there are a number of commands you can use to determine what is appearing on the screen.
The windows spy is good at determining the names of pop up messages. Even if they have the same name as an existing window you can determine which is which by the text included or excluded.
A change of the position of a particular window may be enough to detect the change you are looking for.
Perhaps a bit more detail describing what you are trying to detect will help. It would at least, confirm we are in the same ballpark.
I cannot think of a reliable way to solve this with only built-in functionality. However, you could write it yourself with help of the external GDI+-library.
backgroundColor = 0x644E37
pToken := GDIP_StartUp()
pBitMap := GDIP_BitmapFromScreen()
while(a_index <= a_screenWidth) {
w := a_index
while(a_index <= a_screenHeight) {
h := a_index
ARGB := GDIP_GetPixel(pbitmap, w, h)
RGB := ARGBtoRGB(ARGB)
if(RGB != backgroundColor) {
msgbox, % "found pixel at " w ", " h
}
}
GDIP_DisposeImage(pBitMap)
GDIP_Shutdown(pToken)
return
ARGBtoRGB( ARGB ) {
VarSetCapacity( RGB,6,0 )
DllCall( "msvcrt.dll\sprintf", Str,RGB, Str,"%06X", UInt,ARGB<<8 )
Return "0x" RGB
}
untested
Allegedly, this gdip_bitmapFromScreen() is actually faster than pixelSearch.
source
This pixel search worked for me to find any color other than white:
Search for black (0x000000),
if pixel not found
search for red (0xFF0000),
if pixel not found
search for lime (0x00FF00),
if pixel not found
search for blue (0x0000FF),
if pixel not found, the only color is pure white.
I Set all these to have a variation of 254. A variation of 255 finds everything, including white. I used lime instead of green because the official color called green has a green value of 128, while lime has a green value of 255 (red 0, green 255, blue 0). Seems that just searching for black usually finds NOT white, but sometimes you'll need to work your way down the list. This informs me whether or not there is ANY variation of ANY color other than white, including colors so close to white that I can't see them (when on a white background).
You might have to do a little math and/or experimentation for other colors, but I don't see why this method wouldn't work. I used fast RGB mode and window coordinates. Also I found helpful information here:
https://en.wikipedia.org/wiki/Web_colors
As a relative novice I hate to do this. :)
Right out of PixelSearch in the help file.
PixelSearch, Px, Py, 200, 200, 300, 300, 0x9d6346, 3, Fast
if ErrorLevel
MsgBox, That color was not found in the specified region.
else
MsgBox, A color within 3 shades of variation was found at X%Px% Y%Py%.
Understanding of hexadecimal and how the number applies to the color is necessary.
Color is expressed as a hexadecimal number. In this case 9d6346 and is broken down into three parts. I seem to recall 9d is the value for red, 63 for green, and 46 for blue. Those three numbers can range between Hex 00 to Hex FF (Decimal 0 to 255).
Variation is expressed as a Decimal number is the amount of allowable range in the search color
9D range being 9b to 9f. 63 = 61 to 66, 46 = 44 to 69. Depending on whether the search number is taken in or not my range could be out by 1.
If all else fails Google hexadecimal
Similar possibilities with PixelGetColor followed by a variety of If statements
Be prepared for a very wide variation. Some apps intermingle several distinctly different colors to achieve a desired color.
I have a script which shows different messages depending on if a particular shade of red or green colours are detected, at set screen coordinates. The green detection is working fine, but the red detection is not working.
The script searches the specified x and y coordinate area which is set as: x1, y1, x2, y2.
Scripts seen below.
The hex colour for the green shade is #32CD32, and for red it SEEMS to be #FF0000 (both determined by hovering over the image using Active Window Spy).
Red text screenshot:
[![Red text to be detected][1]][1]
Green text screenshot:
[![Green text that is already successfully detected][2]][2]
My script is detecting green (#32CD32) perfectly. It will NOT detect the red colour #FF0000. I have tried many different variations of the red colour, and still cannot get the script to recognize it when it is on screen. Does anyone know how I can solve this?
This second script to detect RED does not work (note that the coordinates for both scripts aren't the same- the two colours are in different places on the screen):
^t::
Am I making a mistake with the hex colour of the red text? I know that the method overall works fine for green, so it should be straightforward for red.
This code searches the specified rectangle on the screen for a red pixel:
PixelSearch, Px, Py, 121, 282, 169, 310, 0xFF0000, 3, Fast RGB
AutoHotKey expects colors to be specified using a Blue-Green-Red (BGR) format unless you specify the RGB parameter. See the documentation for PixelSearch
I am working on replacing certain color in image by User's selected color. I am using OpenCV for color replacement.
Here in short I have described from where I took help and what I got.
How to change a particular color in an image?
I have followed the step or taken basic idea from answer of above link. In correct answer of that link that guy told you only need to change hue for colour replacement.
after that I run into the issue similar like
color replacement in image for iphone application (i.e. It's good code for color replacement for those who are completely beginners)
from that issue I got the idea that I also need to change "Saturation" also.
Now I am running into issues like
"When my source image is too light(i.e. with high brightness) and I am replacing colour with some dark colour then colours looks light in replaced image instead of dark due to that it seems like Replaced colour does not match with colour using that we done replacement"
This happens because I am not considering the brightness in replacement. Here I am stuck what is the formula or idea to change brightness?
Suppose I am replacing the brightness of image with brightness of destination colour then It would look like flat replacemnt and image will lose it's actual shadow or edges.
Edit:
When I am considering the brightness of source(i.e. the pixel to be processed) in replacment then I am facing one issue. let me explain as per scenario of my application.
for example I am changing the colour of car(like whiteAngl explain) after that I am erasing few portion of the newly coloured car. Again I am doing recolour on erased portion but now what happended is colour done after erase and colour before erase doesn't match because both time I am getting different lightness because both time my pixel of to be processed is changed and due to that lightness of colour changed in output. How to overcome this issue
Any help will be appreciated
Without seeing the code you have tried, it's not easy to guess what you have done wrong. To show you with a concrete example how this is done let's change the ugly blue color of this car:
This short python script shows how we can change the color using the HSV color space:
import cv2
orig = cv2.imread("original.jpg")
hsv = cv2.cvtColor(orig, cv2.COLOR_BGR2HSV)
hsv[:,:,0] += 100
bgr = cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR)
cv2.imwrite('changed.jpg', bgr)
and you get:
On wikipedia you see the hue is between 0 to 360 degrees but for the values in OpenCV see the documentation. You see I added 100 to hue of every pixel in the image. I guess you want to change the color of a portion of your image, but probably you get the idea from the above script.
Here is how to get the requested dark red car. First we get the red one:
The dark red one that I tried to keep the metallic feeling in it:
As I said, the equation you use to shift the light of the color depends on the material you want to have for the object. Here I came up with a quick and dirty equation to keep the metallic material of the car. This script produces the above dark red car image from the first light blue car image:
import cv2
orig = cv2.imread("original.jpg")
hls = cv2.cvtColor(orig, cv2.COLOR_BGR2HLS)
hls[:,:,0] += 80 # change color from blue to red, hue
for i in range(1,50): # 50 times reduce lightness
# select indices where lightness is greater than 0 (black) and less than very bright
# 220-i*2 is there to reduce lightness of bright pixel fewer number of times (than 50 times),
# so in the first iteration we don't reduce lightness of pixels which have lightness >= 200, in the second iteration we don't touch pixels with lightness >= 198 and so on
ind = (hls[:,:,1] > 0) & (hls[:,:,1] < (220-i*2))
# from the lightness of the selected pixels we subtract 1, using trick true=1 false=0
# so the selected pixels get darker
hls[:,:,1] -= ind
bgr = cv2.cvtColor(hls, cv2.COLOR_HLS2BGR)
cv2.imwrite('changed.jpg', bgr)
You are right : changing only the hue will not change the brightness at all (or very weakly due to some perceptual effects), and what you want is to change the brightness as well. And as you mentioned, setting the brightness to the target brightness will loose all pixel values (you will only see changes in saturation). So what's next ?
What you can do is to change the pixel's hue plus try to match the average lightness. To do that, just compute the average brightness B of all your pixels to be processed, and then multiply all your brightness values by Bt/B where Bt is the brightness of your target color.
Doing that will both match the hue (due to the first step) and the brightness due to the second step, while preserving the edges (because you only modified the average brightness).
This is a special case of histogram matching, where here, your target histogram has a single value (the target color) so only the mean can be matched in a reasonable way.
And if you're looking for a "credible source" as stated in your bounty request, I am a postdoc at Harvard and will be presenting a paper on color histogram matching at Siggraph this year ;) .
I know how get a random UIColor, but I wonder if exist a way to only return colors with some specific quality of it. I don't know if exist a specific term, but for example, I need to get only opaque colors, dark tones... colors that are good for backgrounds with bright text/ images on it.
I could hand-pick some, but in my case i want to provide a very good defaults for a large list of icons backgrounds.
UPDATE:
My solution based on the answer here:
float r = arc4random() % 12;
return [UIColor colorWithHue:(30*r)/360 saturation:0.5f brightness:0.8f alpha:1.0f];
I get a very small list of possible answers, but this give me a good start.
What I would do is use the HSV/HSL colour space and then convert the results to RGB. Keep a constant value and saturation (S and V) whose qualities you like, and then alter the hue (H) value for the number of colours you want. Convert the results to RGB and you'll have a nice set of colours that appear to work well together. You can create any set of nice colours that are 'saturated', 'bright', 'dark', etc. just by picking nice combinations of S and V for your needs (low V is dark, high V is bright, low S is unsaturated, high S is saturated).
Take a look at this wiki page for a discussion on converting HSV to RGB. I'd recommend you start with saturation (S) at 0.5 and value (V) at 0.8. Then, to generate nice colours, do the HSV -> RGB conversion for H values of 0, 30, 60, 90... all the way up to 360. It will generate a nice palette of colours that look good together. Later, you can adjust your S and V values if you want different colour 'qualities'.
Using a random color as a background is indeed a suboptimal solution as it will lead to visibility and readability problems of the text and information of your icon. There is some research going on in automatically finding an appropriate color that matches your foreground colors, leading to a harmonic result in terms of visual perception of the image. Have a look at http://cs.nyu.edu/~sorkine/ProjectPages/Harmonization/
For image scanning purposes, I'd like a pixel (which I can get from a UIImage) to match (for a certain percentage) to a pre-set color.
Say pink. When I scan the image for pixels that are pink, I want a function to return a percentage of how much the RGB value in the pixel looks like my pre-set RGB value. This way I'd like all (well, most) pink pixels to become 'visible' to me, not just exact matches.
Is anyone familiar with such an approach? How would you do something like this?
Thanks in advance.
UPDATE: thank you all for your answers so far. I accepted the answer from Damien Pollet because it helped me further and I came to the conclusion that calculating the vector difference between two RGB colors does it perfectly for me (at this moment). It might need some tweaking over time but for now I use the following (in objective c):
float difference = pow( pow((red1 - red2), 2) + pow((green1 - green2), 2) + pow((blue1 - blue2), 2), 0.5 );
If this difference is below 85, I accept the color as my target color. Since my algorithm needs no precision, I'm ok with this solution :)
UPDATE 2: on my search for more I found the following URL which might be quite (understatement) useful for you if you are looking for something similar.
http://www.sunsetlakesoftware.com/2010/10/22/gpu-accelerated-video-processing-mac-and-ios
I would say just compute the vector difference to your target color, and check that it's norm is less than some threshold. I suspect some color spaces are better than others at this, maybe HSL or L*ab, since they separate the brightness from the color hue itself, and so might represent a small perceptual difference by a smaller color vector...
Also, see this related question
Scientific answer: You should convert both colors to the LAB color space and calculate the euclidian distance there. That value is also called deltaE.
The LAB space was developed (using test persons) for exactly that reaason: so that different color pairs with equal distances in tnis space correspond to equal perceived color differences.
However, it sounds like you are not looking for matching a specific color, but rather a color range (lets say all skin tones). That might require more user input than just a reference color + a deltaE tollerance:
a reference color with 3 tollerances for hue, saturation and brightness
a cloud of refence color samples
...