How to get white color in PyQT - 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)

Related

How to fill cell with 50% intensity of an hexadecimal color using openpyxl

I'm using openpyxl with python 3.10 to create a xls file from a dataBase.
I extract an hexadacimal color from this dataBase and I want to fill cells with this color but only with 50% intensity. Currently, i have this :
import sqlite3, openpyxl
def getRouteColor(route):
cursor.execute('''SELECT route_color
FROM routes
WHERE route_short_name = "{}"'''.format(route))
return cursor.fetchall()[0][0]
[...]
color = getRouteColor(route) #hexadecimal
for column in sheet.columns:
for cell in column:
if not cell.row % 2:
cell.fill = openpyxl.styles.PatternFill(patternType = 'solid', fgColor = color)
I've try to use different patternType to lower intensity but it wasn't conclusive because I don't want lines or dots.
Do you have any ideas of how to get a 50% intensity (using openpyxl, or other way like get a 50% intensity hexadecimal code from the 100% intensity hexadecimal code) ?
I have found a way to do it :
I convert my hexadecimal color to RGB color and apply an alpha of 0.5 to it. Then, I convert it back to hexadecimal :
def color50(color): #color : hexadecimal 'XXXXXX'
rgb = tuple(int(color[i:i+2], 16) for i in (0, 2, 4))
rgb50 = []
for i in rgb :
rgb50.append(int(0.5 * i + (1 - 0.5) * 255))
return '{:X}{:X}{:X}'.format(rgb50[0], rgb50[1], rgb50[2])
If you have a better way to do it I'm still interested

Create a colour histogram from an image file

I'd like to use Nim to check the results of my Puppeteer test run executions.
Part of the end result is a screenshot. That screenshot should contain a certain amount of active colours. An active colour being orange, blue, red, or green. They indicate activity is present in the incoming data. Black, grey, and white need to be excluded, they only represent static data.
I haven't found a solution I can use yet.
import stb_image/read as stbi
var
w, h , c:int
data: seq[uint8]
cBin: array[256,int] #colour range was 0->255 afaict
data = stbi.load("screenshot.png",w,h,c,stbi.Default)
for d in data:
cBin[(int)d] = cBin[(int)d] + 1
echo cBin
Now I have a uint array, which I can see I can use to construct a histogram of the values, but I don't know how to map these to something like RGB values. Pointers anyone?
Is there a better package which has this automagically, I didn't spot one.
stbi.load() will return a sequence of interleaved uint8 color components. The number of interleaved components is determined either by c (i.e. channels_in_file) or desired_channels when it is non-zero.
For example, when channels_in_file == stbi.RGB and desired_channels == stbi.Default there are 3 interleaved components of red, green, and blue.
[
# r g b
255, 0, 0, # Pixel 1
0, 255, 0, # Pixel 2
0, 0, 255, # Pixel 3
]
You can process the above like:
import colors
for i in countUp(0, data.len - 3, step = stbi.RGB):
let
r = data[i + 0]
g = data[i + 1]
b = data[i + 2]
pixelColor = colors.rgb(r, g, b)
echo pixelColor
You can read more on this within comments for the stb_image.h.

How to change color of a cell in Excel file with Iron Python?

How do I change cell background color in Excel from Iron Python?
I tried following code:
def rgb_to_hex(rgb):
strValue = '%02x%02x%02x' % rgb
iValue = int(strValue, 16)
return iValue
worksheet.range["H6"].interior.color = rgb_to_hex((255,255,0))
But does not work. It is giving HResult error
I am not familiar with Iron Python, but in C# I do the following:
Range colorCells = WS.get_Range(WS.Cells[row.Row, locCol], WS.Cells[row.Row, locCol + locWid]);
colorCells.Interior.Color = System.Drawing.ColorTranslator.ToOle(color);
My guess is that Tim has the right suggestion, you need to use a different way to convert your RGB to a color that interior.color can accept.
In my C#, it is assigning the color as an int.
public static int ToOle(Color c);
I've managed to enter the color in two ways:
Using ColorIndex
https://learn.microsoft.com/en-us/office/vba/api/excel.colorindex
worksheet.Range["H6"].Interior.ColorIndex = 3
With System.Drawing.ColorTranslator.ToOle(color), like Owen Ivory suggested
clr.AddReference('System.Drawing')
from System.Drawing import Color, ColorTranslator
def rgbForExcel(r, g, b):
return ColorTranslator.ToOle(Color.FromArgb(r, g, b))
worksheet.Range["H6"].Interior.Color = rgbForExcel(255, 50, 0)

CCParticleExplosion color?

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)

How to use HSLA colors with RMagick?

How to set an arbitrary HSLA color instead of gray20?
draw = Magick::Draw.new
draw.font_family = 'arial'
draw.pointsize = 12
draw.gravity = Magick::CenterGravity
draw.annotate(#canvas, size,size, x,y, text) { self.fill = 'gray20' }
Also, using gc.rectangle, how to set the HSLA color of the fill?
gc = Magick::Draw.new
gc.fill ????
gc.rectangle(x,y, x + size,y + size)
From the RMagick documentation:
Many RMagick methods expect color name arguments or return color names. A color name can be
an X11 color name such as "red", "chocolate", or "lightslategray".
an SVG color name (similar to the X color names), or
a string in one of the formats shown in the following table.
...
hsla(h,s,l,a)
And HSL documentation
hsla(33.3333%, 100%, 50%, 1.0) green with an alpha value of 1.0
hsla(120, 255, 127.5, 1.0) green with an alpha value of 1.0
So, use a string: fill 'hsl(0%,100%,100%,1)'

Resources