Kivy not responding to RGBA values - kivy

While giving the background color attribute in the .kv file for a button, and the value in two of the rgba fields goes above 220.0/255, it turns back to the default grey color.
My code excerpt with the FloatLayout
Button:
size_hint : 0.95/18, 0.1
pos_hint : {"center_x" : 0.95, "center_y" : 0.925}
font : 40
background_color: 1.0, 225.0/255, 228.0/255, 0
color: 211.0/255, 54.0/255, 112.0/255, 1
text: "Sample Text"
halign : "center"
I tried decreasing the values of the rgb fields and its working, but it isn't giving the color above the 220.0/255 value. I tried converting it to up to two decimal float, and it is still returning the same.

Related

How to use Color with RGB or hex value in SwiftUI using Swift 5? [duplicate]

This question already has answers here:
How can I create a UIColor from a hex string?
(49 answers)
Closed 2 years ago.
The community reviewed whether to reopen this question 12 months ago and left it closed:
Original close reason(s) were not resolved
I am currently trying to change the background color of a Label in SwiftUI. As far as I know, SwiftUI takes a View as the parameter for the background Modifier.
However, I encountered a problem I am not able to fix:
I would like to change the background color of my Label by using a hex code or an RGB value. Because of some reason, when doing so the background color always changes to white.
This works perfectly fine:
Label(
title: { Text("someTitle") },
icon: { Image(systemName: "someName") }
)
.background(Color.purple) // Using the provided standard Colors works perfectly fine.
However, I would like to create a background color using a hex or RGB value:
.background(Color(Color.RGBColorSpace.sRGB, red: 92, green: 225, blue: 230, opacity: 1))
// or
.background(Color(UIColor(red: 220, green: 24, blue: 311, alpha: 1)))
This does not work and the background color of the Label always changes back to white. How to achieve this goal?
The UIColor init method UIColor(red:green:blue:alpha) takes float values from 0 to 1 for each value.
Any value ≥ 1.0 is going to "max out" that color. So an expression like UIColor(red: 220, green: 24, blue: 311, alpha: 1) will set all 3 color channels and alpha to 1.0, resulting in white. So would UIColor(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
You could create an extension to UIColor that adds convenience initializers that would take values of different types. First, and initializer that would take values from 0-255 for each. Then you could create another intializer UIColor.init(hexString:String).
The one that took values from 0-255 would be a lot easier to write. The one that took hex strings would involve implementing a hex parser. I bet somebody has already written it though.
Take a look at this code, for example:
https://gist.github.com/anonymous/fd07ecf47591c9f9ed1a

Swift get UITextField placeholder label color

I want to know what color is used for text field placeholder.
I have project that use default text field with default placeholder in many times. Also i need to implement UI elements that use custom placeholder label. Unfortunately, i can't find any information about color specification of UITextField placeholder.
Can someone provide RGB or HEX value for this? Thanks.
The attributedPlaceholder property of UITextField is a NSAttributedString. Querying this string's .foregroundColor attribute yields UIExtendedSRGBColorSpace 0 0 0.0980392 0.22.
So the color seems to be:
let color = UIColor(red: 0, green: 0, blue: 0.0980392, alpha: 0.22)
Xcode can pick color from editor #_#
the color is very closed to Magnesium in Crayons panel of color picker,
hex is C7C7CD, Magnesium is C0C0C0, I like regular number, suggest you use C0C0C0 instead, really the same

Plot array with 3 colors in Amibroker

I am using Amibroker. I would like to make a plot of the array PCT_CLOSE such that the color is red when PCT_CLOSE<=25, yellow when between 25 and 50, Green when PCT_CLOSE>=50.
Right now, due to the IIF function constraint, I can only create a plot with 2 colors. Below is how I did it with 2 colors.
Plot( PCT_CLOSE , "CLOSE", IIf(PCT_CLOSE<=50, colorRed, colorYellow), styleNoTitle | styleLine | styleThick );
I will answer my own question.
Color criteria in question:
color is red when PCT_CLOSE<=25, yellow when between 25 and 50, Green
when PCT_CLOSE>=50
The key is to have nested IIF statements. Put IIF inside IIF.
Here is the corresponding code;
color = IIf(PCT_CLOSE<=25, colorRed, IIf(PCT_CLOSE>50, colorGreen, colorYellow) )

RGB and conditional instruction in .kv file

So I've been working on an application, and tried to use a conditional kv instruction in kv, as described here.
<myWidget#Widget>:
size: self.parent.width-self.height*0.5, self.parent.height/12
selected: False
canvas:
Color:
rgb: 1,1,0 if self.selected else 1,1,1
Rectangle:
pos: self.pos
size: self.parent.width-self.height*0.5, self.parent.height/12
My problem is that I don't understand the rgb behaviour : while the colours in this example work fine (turns to yellow when selected and back to white when unselected), other colour combinations will give unpredictable colors, or no result at all.
Could someone explain to me what actually happens to the rgb property ?
This is being parsed as (1, 1, (0 if self.selected else 1), 1, 1), which obviously isn't what you want but (as you note) happens to work in the case of yellow/white. Try instead
rgb: (1,1,0) if self.selected else (1,1,1)

UINavigationBar background color setted from storyboard differ from the color setted by code

My app will be available only for iOS 8 and I'm working with XCode 6.1.1.
If I set the color through the storyboard (setting theBar Tint attribute on Navigation Bar section)
The desired color is:
56 186 145
I used Core coding utilities to get the floating values of my color.
By code:
let backgroundColor = UIColor(red: 0.22, green: 0.729, blue: 0.569, alpha: 1.0)
self.navigationController!.navigationBar.barTintColor = backgroundColor
self.navigationController!.navigationBar.translucent = false
Color setted through storyboard is the same as the Original RGB.
By code:
By storyboard:
EDIT
I tried using let backgroundColor = UIColor(red: 0.255, green: 0.769, blue: 0.635, alpha: 1.0) and rgb from storyboard: 65 196 162 based on #siejkowski comment, but I get these colors:
By code:
By storyboard:
Why?
There is two reason for the color differences here you observed.
The value set in Storyboard is RGB (sRGB IEC61966-2.1) type and when you code UIColor by RGB value it will return RGB (Generic RGB) values.
So when you change color from Storyboard the values are different for RBG types. To get exact RGB value change type of RGB Sliders in Storyboard.
Click on Settings icon which is exactly right to the RGB Sliders Option. It will show a pop-up menu - select Generic RGB option.
Now you can observe an image that values for RGB
56 186 145
is now change to
49 175 126
So these are desire values to code.
Roundup issue:
In code, you are passing giving round up values for parameter like in below line
UIColor(red: 0.22, green: 0.729, blue: 0.569, alpha: 1.0)
So it will make a small change per pixel of the color code. I suggest you divide this values by 255 and leave round up calculation for the compiler. Which will gives you desire color accuracy.
So now for new values update code will be:
let backgroundColor = UIColor(red: 49.0/255.0, green: 175.0/255.0, blue: 126.0/255.0, alpha: 1.0)
self.navigationController!.navigationBar.barTintColor = backgroundColor
self.navigationController!.navigationBar.isTranslucent = false
You see the difference in colors because they are set using the same RGB values, but in different color-spaces.
UIColor, when set from code, interprets the values as from sRGB space, as provided here: https://developer.apple.com/library/ios/documentation/GraphicsImaging/Reference/CGColorSpace/index.html
However, it seems that the Storyboard color picker is using native values RGB by default. You need to convert between the two. You can do it in Storyboard color picker by clicking the settings icon next to RGB Sliders list picker in Colors tab. Native RGB values are set under Color LCD setting and sRGB values are under sRGB, of course.
When you do it, is seems that 56 186 145 in native RGB space is 73 186 141 in sRGB space. So your code needs to be:
let backgroundColor = UIColor(red: 0.286, green:0.729, blue:0.553, alpha:1.0)
When I try to change the color space to GenericRGB from the storyboard/Interface builder (on XCode 6.3.2), XCode does not save the change. So if I reopen the color from Interface builder the color space is always sRGBIEC61966-2.1.
So I came up a different solution that may be useful for someone else:
with the Mac OS default app "ColorSync Utility" I used the Calculator and translate the value of my color from GenericRGB to a sRGBIEC61966-2.1. Then I set the translated value into the Interface builder and I obtained the same result as was obtaining it programmatically!

Resources