I am doing a HSV color picker for Krita that has PyQt5.
I wanted to make use of the QColor class to take care of display color conversions, however i am not able to convert the color with success.
I used this color selector as a reference for my output:
https://www.google.com/search?sxsrf=ACYBGNR9_2R1jGyxkqbdM8DVZfU-8hogYg%3A1583142187803&source=hp&ei=K9VcXqrKLuyMlwTorLH4Dg&q=color+picker+hex&oq=color+picker&gs_l=psy-ab.3.0.35i39l2j0l8.3060.6761..8026...2.0..0.310.1102.11j3-1......0....1..gws-wiz.....10..35i362i39j0i10.C-faeoukSfM
a random color of choice was:
HEX=(#eb4034)
HSV=(7, 78, 92)
When I try to convert it with this code:
from PyQt5.QtGui import QColor
hsv = QColor.fromHsv(7,78,92,255)
color = str(hsv.redF()*255)+" | "+str(hsv.greenF()*255)+" | "+str(hsv.blueF()*255)
print(str( color ))
my output is:
92.0 | 67.14007782101167 | 63.85992217898833
[Finished in 0.168s]
instead of:
235, 64, 52
How do I input a HSV color into the QColor and then convert it to RGB for display purposes?
thank you in advance
The reference values for the HVS color (i.e. (7, 78, 92)) are in units degree, percent, and percent, respectively, whereas QColor.fromHSV expects values in the range 0-255. To convert the reference values to something that can be used by QColor you could try something like this
HSV=(7, 78, 92)
hue, sat, value = HSV
hue = int(hue*255/360)
sat = int(sat*255/100)
value = int(value*255/100)
hsv = QColor.fromHsv(hue, sat, value, 255)
color = str(hsv.redF()*255)+" | "+str(hsv.greenF()*255)+" | "+str(hsv.blueF()*255)
print(str( color ))
# output: 234.0 | 64.42023346303502 | 52.307392996108945
You can use the getRgb() method to get a tuple with the values:
>>> hsv = QColor.fromHsv(7,78,92,255)
>>> hsv.getRgb()
(92, 67, 64, 255)
You will notice the values are different than what you expected, this is because the color hex #eb4034 is not the same as HSV (7, 78, 92).
>>> hsv.name()
'#5c4340'
>>> QColor('#eb4034').getHsv()
(3, 199, 235, 255)
Make sure you are using the correct HSV values.
Related
I'm trying to add a colormap to a TMS service which is serving single band PNG with values ranging from 1 to 11. At this point, the layer renders in black (low values between 1 and 11) but I would like it to render with a specific color for each of the 11 values. This is for a QGIS plugin that adds layer to the map.
Here is a sample of my code, any help would be very much appreciated!
# Create rlayer
urlWithParams = 'type=xyz&url=https://bucket_name.s3.ca-central-1.amazonaws.com/z/x/-y.png&zmax=19&zmin=0&crs=EPSG3857'
layerName = 'Classified image'
rlayer = QgsRasterLayer(urlWithParams, layerName, 'wms')
# One of my attempt to create the renderer
fcn = QgsColorRampShader()
fcn.setColorRampType(QgsColorRampShader.Discrete)
lst = [QgsColorRampShader.ColorRampItem(1, QColor(0, 255, 0)),
QgsColorRampShader.ColorRampItem(2, QColor(65, 123, 23)),
QgsColorRampShader.ColorRampItem(3, QColor(123, 76, 34)),
QgsColorRampShader.ColorRampItem(4, QColor(45, 234, 223)),
QgsColorRampShader.ColorRampItem(5, QColor(90, 134, 23)),
QgsColorRampShader.ColorRampItem(6, QColor(45, 255, 156)),
QgsColorRampShader.ColorRampItem(7, QColor(245, 23, 123)),
QgsColorRampShader.ColorRampItem(8, QColor(233, 167, 87)),
QgsColorRampShader.ColorRampItem(9, QColor(123, 125, 23)),
QgsColorRampShader.ColorRampItem(10, QColor(213, 231, 123)),
QgsColorRampShader.ColorRampItem(11, QColor(255, 255, 0))]
fcn.setColorRampItemList(lst)
shader = QgsRasterShader()
shader.setRasterShaderFunction(fcn)
renderer = QgsSingleBandColorDataRenderer(rlayer.dataProvider(), 1, shader)
rlayer.setRenderer(renderer)
rlayer.triggerRepaint()
# Add rendered layer to QGIS map
QgsProject.instance().addMapLayer(rlayer)
It looks like the type of renderer is QgsSingleBandColorDataRenderer. Any idea how to make this work? Thanks!
I have a beginner question for numpy arrays. Why does the output have random spacing in between the elements and commas?
input:
list_weight_pounds = [150, 140, 220, 205, 265]
array_weight_pounds = np.array([150, 140, 220, 205, 265])
array_weight_kg = array_weight_pounds / 2.2046
array_weight_kg
output
array([ 68.03955366, 63.50358342, 99.79134537, 92.98739 ,
120.20321147])
this is because the default floatmode is 'maxprec':
‘maxprec’: Print at most precision fractional digits, but if an element can be uniquely represented with fewer digits only print it with that many.
You can set it to 'fixed' to get trailing zeroes instead of spaces:
np.set_printoptions(floatmode='fixed')
array_weight_kg
#array([ 68.03955366, 63.50358342, 99.79134537, 92.98739000,
# 120.20321147])
You can also use the printoptions context manager. See set_printoptions for further formatting options.
Is it possible to convert a 3d vector representing a colour into a single numerical value (x)? Something ideally that is a float value between 0 and 1. Math's is not my strong suit at all so from my googling I think I either need to use vectorization or convert the value to a tensor to achieve my objective. Would that be correct?
An example of what I am trying to achieve is:
labColour = (112, 48, 0)
labAsFloat = colour_to_float(luvColour, cspace='LAB')
print(labAsFloat) # outputs something like 0.74673543
def colour_to_float(colour, cspace):
return ??? somehow vectorise??
Not quite sure I understand your question correctly. If the objective is merely a unique floating number representation then this might work.
def colour_to_float(colour):
int_arr = list(colour)
int_arr.append(0)
data_bytes = np.array(int_arr, dtype=np.uint8)
return (data_bytes.view(dtype=np.float32))[0]
def float_to_colour(num):
return np.array([num], dtype=np.float32).view(dtype=np.uint8)[:3].tolist()
Results:
labColour = (230, 140, 50)
f = colour_to_float(labColour)
print(f)
4.64232e-39
lab = float_to_colour(f)
print(lab)
[230, 140, 50]
A project I previously submitted for a course worked as expected. I went back to run the code again and now get an python traceback error message that didn't occur before:
'matplotlib.pyplot' has no attribute 'autofmt_xdate'
I loaded the weather station data files and ran all the code, which previously worked. Below is the code for the visualization plot:
plt.figure()
plt.plot(minmaxdf.loc[:,'Month-Day'], minmaxdf.loc[:,'min_tmps'] ,'-', c = 'cyan', linewidth=0.5, label = '10yr record lows')
plt.plot(minmaxdf.loc[:,'Month-Day'], minmaxdf.loc[:,'max_tmps'] , '-', c = 'orange', linewidth=0.5, label = '10yr record highs')
plt.gca().fill_between(range(len(minmaxdf.loc[:,'min_tmps'])), minmaxdf['min_tmps'], minmaxdf['max_tmps'], facecolor = (0.5, 0.5, 0.5), alpha = 0.5)
plt.scatter(minbreach15.loc[:,'Month-Day'], minbreach15.loc[:,'min_tmps_breach15'], s = 10, c = 'blue', label = 'Record low broken - 2015')
plt.scatter(maxbreach15.loc[:,'Month-Day'], maxbreach15.loc[:,'max_tmps_breach15'], s = 10, c = 'red', label = 'Record high broken - 2015')
plt.xlabel('Month')
plt.ylabel('Temperature (Tenths of Degrees C)')
plt.title('10yr Max/Min Temperature Range for Wilton CT 06897')
plt.gca().axis([0, 400, -500, 500])
plt.xticks(range(0, len(minmaxdf.loc[:,'Month-Day']), 30), minmaxdf.loc[:,'Month-Day'].index[range(0, len(minmaxdf.loc[:,'Month-Day']), 30)], rotation = '-45')
plt.xticks( np.linspace(0, 15 + 30*11 , num = 12), (r'Jan', r'Feb', r'Mar', r'Apr', r'May', r'Jun', r'Jul', r'Aug', r'Sep', r'Oct', r'Nov', r'Dec') )
plt.legend(loc = 4, frameon = False)
plt.gca().spines['top'].set_visible(False)
plt.gca().spines['right'].set_visible(False)
plt.autofmt_xdate()
plt.show()
produced a chart of day of year (2004-14) 10yr average temp max/mins, overlay with scatter points of 2015 max/mins that exceeded the averages.
autofmt_xdate() is a method of the Figure. The command hence needs to be
plt.gcf().autofmt_xdate()
Code:
g.filter(join_month == "2008.03").nodes.color = red
Why doesn't the following work?
g.filter(join_month == "2008.03").nodes.color = #FFFFFF
Do I just have the format of the hexadecimal color wrong?
Gephi seems to be using Jython as the basis for its Python Interpreter.
You should be able to get your desired result by creating a java.awt.Color object and passing your hex value to the constructor, like so:
>>> from java.awt import Color
>>> mycolor = Color(0xFFFFFF)
>>> g.filter(join_month == "2008.03").nodes.color = mycolor
According to an example found here it looks like another way to do this is to use Gephi's color class.:
>>> red = 0xFF
>>> green = 0xFF
>>> blue = 0xFF
>>> g.filter(join_month == "2008.03").nodes.color = color(red, green, blue)