convert rgba color codes 255,255,255,255 to kivy color codes in 1,1,1,1

Question:

In order to specify any color value in kivy we need to specify values from 0 to 1 i.e.
color defaults to [1,1,1,1] and can contain values between 0 and 1 , however rgba codes which are readily available for other languages are always specified from 0 to 255 and i usually refer them from any web link as in
http://www.rapidtables.com/web/color/RGB_Color.htm

This helps even in various python toolkits example pyQt library however in kivy its different .

Does anyone know why its specified from 0 to 1 and if there s any way to code values used by various other languages to kivy color codes . for example what if i want to change rgb(192,192,192) to kivy color code ?

Asked By: paarth batra

||

Answers:

I think you can just divide it by 255:

float_color = color / 255.0
Answered By: gog

As you can see here, in Kivy the first three numbers are just re-scaled RGB values to 0 and 1, and the last one is alpha. But remember you can set the mode, you could use hsv instead, not I guess you don’t need that.

Answered By: adrin

To avoid some divisions and speed up:

http://www.corecoding.com/utilities/rgb-or-hex-to-float.php

Answered By: DoctorRu

Though this doesnt really answer the question, but another simple way is using kivy’s hex feature , an example in kv lang:

#:import hex kivy.utils.get_color_from_hex
    canvas:
        Color:
            rgba: hex('#03A9F4')
Answered By: Big Zak

Check out kivy.utils for more cool features, found this easy thing, before this I used to go to a site for converting hex to decimal rgb( coz I am too lazy to divide each rgb value by 255.

#:import utils kivy.utils
<MainWindow>:
Button:
background_color: utils.get_color_from_hex('#58AE6F')

The only issue with this approach is that you can’t enter transparency, if you wish to use transparency you can very well use the site I mentioned to get decimals for ‘rgb’ and using percentage of transparency as ‘a’ value in ‘rgba’.

Answered By: Ritveak

You can do the following.

(**255**/255.0, **160**/255.0, **0**/255.0, 1)

So what you need to do is divide your RGB value with 255.0, so the above color will give you orange. All the numbers that have ** will be your RBG Colors.

Answered By: UKzs

I would like to register here a less conventional method, which I think is more organized and clean: Define a color converting function that you can import and use in your .kv file.

Define your function in a func.py module

# color convert
def cc(color):
    return color / 255

Import it and use it in the .kv file

#:import cc func.cc
   
<YourBuildClass>:
    BoxLayout:
        canvas:
            Color:
                rgb: 0, cc(102), cc(173)
            Rectangle:
                pos: self.pos
                size: self.size
Answered By: Bruno Dalagnol

convert from regular rgba to kivy rgba

def krgb(r,g,b,a):
    return (r/255,g/255,b/255,int(a==255),)
Answered By: coolcoder613
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.