Convert UIColor to 24 bit value - ios

I have a bluetooth device (led light) to which I can send different commands to. By now everything works fine and all the commands work. Now I want to send a custom command. The documentation says the following:
[0X99] + [24 bit colorimetric value (R=?,G=?,B=?)] + [8bit speed value] + ...
Now I don't know how to get the 24 bit colorimetric value from an UIColor.

Refer to this SO Post. This shows how to convert uicolor to its rgb component values.The values are returned as a float ratio. You can convert this ration to base 8 color and send to the device.
Hope this helps.

Related

iMA not displaying correctly in backtesting

I developed and started testing an EA in MQL4 that uses iMA function. Basically the program compares the iMA value of the current candle with the iMA value of the previous candle. When I test the EA using the Strategy Tester (Every Tick) my EA is not opening and closing trades correctly. What I mean is the trade does not open on the correct candle. Upon further investigating I noticed, on the current candle the value for iMA in the data window and chart are the same, but they difer from the 'Print' value. The value for the previous candle is correct. When I did a Google search I found that someone in 2008 reported this exact same issue. In 2008 there didn't appear to be a solution.
Now that we are in a new decade, I'm wondering if there is a solution?
Does anyone know if iMA works in MQL5 Strategy Tester?
double MAEMACurrent = iMA(NULL,0,3,0,MODE_EMA,PRICE_CLOSE,0);
double MAEMAPrevious = iMA(NULL,0,3,0,MODE_EMA,PRICE_CLOSE,1);
double MASlowEMACurrent = iMA(NULL,0,10,0,MODE_EMA,PRICE_CLOSE,0);
double MASlowEMAPrevious = iMA(NULL,0,10,0,MODE_EMA,PRICE_CLOSE,1);
Print("MAEMACurrent " + MAEMACurrent + " MAEMAPrevious " + MAEMAPrevious + " MASlowEMACurrent " + MASlowEMACurrent + " MASlowEMAPrevious " + MASlowEMAPrevious);
Chart & Data Window:
MAEMACurrent: 1.95552
MAEMAPrevious: 1.95572
MASlowEMACurrent: 1.95201
MASlowEMAPrevious: 1.95097
Print Value:
MAEMACurrent: 1.95538
MAEMAPrevious: 1.95572
MASlowEMACurrent: 1.951086
MASlowEMAPrevious: 1.950972
As you can see from the above example the 'Chart & Data Window' values for MAEMACurrent and the MASlowEMACurrent do not match 'Print Value'.
This is the first time that I'm asking a question, so if I've missed something or I am not following the correct protocol for asking a question please let me know.
First, always use the "NormalizeDouble" function to round the values to a proper number of fractions. In your case, if there are just 5 digits after fraction, use the following code to round the values of "MASlowEMACurrent" and "MASlowEMAPrevious" to 5 digits:
double dNormalizedValue = NormalizeDouble(MASlowEMACurrent, 5);
In addition, never compare the value of the in-progress candle on the chart with the values which are returned by the indicator or price functions like (iMA, iClose, etc). Please note that even a very slight time difference could cause differences in the two values. For other candles (in your case the previous candle), since they all have been closed and there are no changes in progress so you can compare the values on the chart with the values returned by the functions. So, iMA is working as expected.

Is there a way to know the color (white, black, rose gold, etc) of an iOS device in Swift? [duplicate]

Is there any way or hack to detect on what color (black / white) iPhone, iPad or iPod touch the iOS is installed?
I want to load corresponding UI skins in case of Black or White devices.
There's a private API to retrieve both the DeviceColor and the DeviceEnclosureColor.
UIDevice *device = [UIDevice currentDevice];
SEL selector = NSSelectorFromString(#"deviceInfoForKey:");
if (![device respondsToSelector:selector]) {
selector = NSSelectorFromString(#"_deviceInfoForKey:");
}
if ([device respondsToSelector:selector]) {
NSLog(#"DeviceColor: %# DeviceEnclosureColor: %#", [device performSelector:selector withObject:#"DeviceColor"], [device performSelector:selector withObject:#"DeviceEnclosureColor"]);
}
I've blogged about this and provide a sample app:
http://www.futuretap.com/blog/device-colors/
Warning: As mentioned, this is a private API. Don't use this in App Store builds.
The answer to the question is NO (as of now) and personally I don't think it's worth much, because what if the end-user uses a skin or an additional casing for his iPhone?
I'd suggest to initially ask the user "Hey, what's the color of your phone?" and then do accordingly.
Additionally, a research provided me with this information, I'm not sure if it's TRUE or if is going to help you.
The serial number is the key :)
If aabccdddeef is the serial number of the iPhone 4, ee represents the Color, (A4=black). I hope some of you here check this information with yours to see if this is true.
Just my 2 cents worth - if anyone is looking for the iPhone 5c colors, the colors below are picked from the apple website.
Hope it is of use to anyone:-)
iPhone 5c Colors:
Green
R 179
G 243
B 142
HEX #B3F38E
Blue
R 123
G 195
B 252
HEX #7BC3FC
Yellow
R 255
G 243
B 141
HEX #FFF38D
Red
R 252
G 132
B 142
HEX #FF848E
White
R 239
G 239
B 239
HEX #EFEFEF
According to information sprinkled around the web, the color of the device is encoded in the serial number. iFixit's blog indicates that the third- and second-to-last positions hold the information: xxxxxxxxCCx For an iPhone 4, A4 indicates black. No one seems to have the code for a white iPhone 4, which is strange. One forum posting indicates that it might be DZ. Everyone seems to be just parroting everyone else's information here.
Another site, mydigitallife.com, has an article listing the color codes for various older models. In some cases, the three positions have to be read together in order to determine the color. According to this site, all iPhone 3Gs models have 3N in the color code position; 3NP indicates "Black 16GB 3Gs", and 3NQ indicates "White 16GB 3Gs". The original (2G) iPhone also uses all three positions to indicate size (there were no color options).
There are already a number of questions here on SO that will help you to get the device's serial number.
For debugging purposes I compiled a more comprehensive list of of possible deviceInfoForKey: keys.
Interesting keys to note (for this question) are DeviceRGBColor DeviceEnclosureRGBColor. The values appear to be an integer that represent the RGB value in the form 0x00rrggbb.
Here, for reference, are all the keys I found:
ActiveWirelessTechnology
AirplaneMode
assistant
BasebandCertId
BasebandChipId
BasebandPostponementStatus
BasebandStatus
BatteryCurrentCapacity
BatteryIsCharging
BluetoothAddress
BoardId
BootNonce
BuildVersion
CertificateProductionStatus
CertificateSecurityMode
ChipID
CompassCalibrationDictionary
CPUArchitecture
DeviceClass
DeviceColor
DeviceEnclosureColor
DeviceEnclosureRGBColor
DeviceName
DeviceRGBColor
DeviceSupportsFaceTime
DeviceVariant
DeviceVariantGuess
DiagData
dictation
DiskUsage
EffectiveProductionStatus
EffectiveProductionStatusAp
EffectiveProductionStatusSEP
EffectiveSecurityMode
EffectiveSecurityModeAp
EffectiveSecurityModeSEP
FirmwarePreflightInfo
FirmwareVersion
FrontFacingCameraHFRCapability
HardwarePlatform
HasSEP
HWModelStr
Image4Supported
InternalBuild
InverseDeviceID
ipad
MixAndMatchPrevention
MLBSerialNumber
MobileSubscriberCountryCode
MobileSubscriberNetworkCode
ModelNumber
PartitionType
PasswordProtected
ProductName
ProductType
ProductVersion
ProximitySensorCalibrationDictionary
RearFacingCameraHFRCapability
RegionCode
RegionInfo
SDIOManufacturerTuple
SDIOProductInfo
SerialNumber
SIMTrayStatus
SoftwareBehavior
SoftwareBundleVersion
SupportedDeviceFamilies
SupportedKeyboards
telephony
UniqueChipID
UniqueDeviceID
UserAssignedDeviceName
wifi
WifiVendor
As others have noted, no, there is no official way of getting this information. Apple clearly knows (look in iTunes when you sync), so they could make it available. Might be worth raising a Radar.

RGB Conversion in Lua/TI-Nspire (TI-image)

I know this may be kinda of a dead and old unanswered topic, but
im trying to convert a RGB color code to the string format in a TI-image file, which doesnt make sense to me here:
https://wiki.inspired-lua.org/TI.Image
I understand all it mentions, until I reach the rgb conversion part. The article says that each rgb color has to have 5 bits, but it doesnt tell how to convert it, and i cant make sense of how to convert it following the given example. For instance:
R=255 → R = 31
G=012 → G = 1
B=123 → B = 15
What would I have to do to convert R255, G012 and B123 to the above output?
I understand the remaining instructions on the article, except this.
Anyone have an idea on how to do this?

Is it better to write 0.0, 0.0f or .0f instead of simple 0 for supposed float or double values

Hello well all is in the title. The question apply especially for all those values that can be like NSTimeInterval, CGFloat or any other variable that is a float or a double. Thanks.
EDIT: I'm asking for value assignment not format in a string.
EDIT 2: The question is really does assigning a plain 0 for a float or a double is worst than anything with f a the end.
The basic difference is as :
1.0 or 1. is a double constant
1.0f is a float constant
Without a suffix, a literal with a decimal in it (123.0) will be treated as a double-precision floating-point number. If you assign or pass that to a single-precision variable or parameter, the compiler will (should) issue a warning. Appending f tells the compiler you want the literal to be treated as a single-precision floating-point number.
If you are initializing a variable then it make no sense. compiler does all the cast for you.
float a = 0; //Cast int 0 to float 0.0
float b = 0.0; //Cast 0.0 double to float 0.0 as by default floating point constants are double
float c = 0.0f // Assigning float to float. .0f is same as 0.0f
But if you are using these in an expression then that make a lot of sense.
6/5 becomes 1
6/5.0 becomes 1.2 (double value)
6/5.0f becomes 1.2 (float value)
If you want to dig out if there is any difference to the target CPU running the code or the binary code it executes, you can easily copy one of the command lines compiling the code from XCode to command line, fix missing environment variables and add a -S. By that you would get assembly output, that you can use to compare. If you put all 4 variants in a small example source file, you can compare the resulting assembly code afterwards, even without being fluent in ARM assembly.
From my ARM assembly experience (okay... 6 years ago and GCC) I would bet 1ct on something like XORing a register with itself to flush it's content to 0.
Whether you use 0.0, .0, or 0.0f or even 0f does not make much of a difference. (There are some with respect to double and float) You may even use (float) 0.
But there is a significant difference between 0 and some float notation. Zero will always be some type of integer. And that can force the machine to perform integer operations when you may want float operations instead.
I do not have a good example for zero handy but I've got one for float/int in general, which nealy drove me crazy the other day.
I am used to 8-Bit-RGB colors That is because of my hobby as photographer and because of my recent background as html developer. So I felt it difficult to get used to the cocoa style 0..1 fractions of red, green and yellow. To overcome that I wanted to use the values that I was used to and devide them by 255.
[CGColor colorWithRed: 128/255 green: 128/255 andYellow: 128/255];
That should generate me some nice middle gray. But it did not. All that I tried either made a black or white.
First I thought that this was caused by some undocumented dificiency of the UI text objects with which I was using this colour. It took a while to realize that this constant values forced integer operations wich can only round up or down to 0 and 1.
This expession eventually did what I wanted to achieve:
[CGColor colorWithRed: 128.0/255.0 green: 128.0/255.0 andYellow: 128.0/255.0];
You could achieve the same thing with less .0s attached. But it does not hurt having more of them as needed. 128.0f/(float)255 would do either.
Edit to respond to your "Edit2":
float fvar;
fvar = 0;
vs ...
fvar = .0;
In the end it does not make a difference at all. fvar will contain a float value close to (but not always equal to) 0.0. For compilers in the 60th and 70th I would have guessed that there is a minor performance issue associated with fvar = 0. That is that the compiler creates an int 0 first which will then have to be converted to float before the assignment. Modern compilers of today should optimize automatically much better than older ones. In the end I'd have to look at the machine code output to see whether it does make a difference.
However, with fvar = .0; you are always on the safe site.

Detecting Color of iPhone/iPad/iPod touch?

Is there any way or hack to detect on what color (black / white) iPhone, iPad or iPod touch the iOS is installed?
I want to load corresponding UI skins in case of Black or White devices.
There's a private API to retrieve both the DeviceColor and the DeviceEnclosureColor.
UIDevice *device = [UIDevice currentDevice];
SEL selector = NSSelectorFromString(#"deviceInfoForKey:");
if (![device respondsToSelector:selector]) {
selector = NSSelectorFromString(#"_deviceInfoForKey:");
}
if ([device respondsToSelector:selector]) {
NSLog(#"DeviceColor: %# DeviceEnclosureColor: %#", [device performSelector:selector withObject:#"DeviceColor"], [device performSelector:selector withObject:#"DeviceEnclosureColor"]);
}
I've blogged about this and provide a sample app:
http://www.futuretap.com/blog/device-colors/
Warning: As mentioned, this is a private API. Don't use this in App Store builds.
The answer to the question is NO (as of now) and personally I don't think it's worth much, because what if the end-user uses a skin or an additional casing for his iPhone?
I'd suggest to initially ask the user "Hey, what's the color of your phone?" and then do accordingly.
Additionally, a research provided me with this information, I'm not sure if it's TRUE or if is going to help you.
The serial number is the key :)
If aabccdddeef is the serial number of the iPhone 4, ee represents the Color, (A4=black). I hope some of you here check this information with yours to see if this is true.
Just my 2 cents worth - if anyone is looking for the iPhone 5c colors, the colors below are picked from the apple website.
Hope it is of use to anyone:-)
iPhone 5c Colors:
Green
R 179
G 243
B 142
HEX #B3F38E
Blue
R 123
G 195
B 252
HEX #7BC3FC
Yellow
R 255
G 243
B 141
HEX #FFF38D
Red
R 252
G 132
B 142
HEX #FF848E
White
R 239
G 239
B 239
HEX #EFEFEF
According to information sprinkled around the web, the color of the device is encoded in the serial number. iFixit's blog indicates that the third- and second-to-last positions hold the information: xxxxxxxxCCx For an iPhone 4, A4 indicates black. No one seems to have the code for a white iPhone 4, which is strange. One forum posting indicates that it might be DZ. Everyone seems to be just parroting everyone else's information here.
Another site, mydigitallife.com, has an article listing the color codes for various older models. In some cases, the three positions have to be read together in order to determine the color. According to this site, all iPhone 3Gs models have 3N in the color code position; 3NP indicates "Black 16GB 3Gs", and 3NQ indicates "White 16GB 3Gs". The original (2G) iPhone also uses all three positions to indicate size (there were no color options).
There are already a number of questions here on SO that will help you to get the device's serial number.
For debugging purposes I compiled a more comprehensive list of of possible deviceInfoForKey: keys.
Interesting keys to note (for this question) are DeviceRGBColor DeviceEnclosureRGBColor. The values appear to be an integer that represent the RGB value in the form 0x00rrggbb.
Here, for reference, are all the keys I found:
ActiveWirelessTechnology
AirplaneMode
assistant
BasebandCertId
BasebandChipId
BasebandPostponementStatus
BasebandStatus
BatteryCurrentCapacity
BatteryIsCharging
BluetoothAddress
BoardId
BootNonce
BuildVersion
CertificateProductionStatus
CertificateSecurityMode
ChipID
CompassCalibrationDictionary
CPUArchitecture
DeviceClass
DeviceColor
DeviceEnclosureColor
DeviceEnclosureRGBColor
DeviceName
DeviceRGBColor
DeviceSupportsFaceTime
DeviceVariant
DeviceVariantGuess
DiagData
dictation
DiskUsage
EffectiveProductionStatus
EffectiveProductionStatusAp
EffectiveProductionStatusSEP
EffectiveSecurityMode
EffectiveSecurityModeAp
EffectiveSecurityModeSEP
FirmwarePreflightInfo
FirmwareVersion
FrontFacingCameraHFRCapability
HardwarePlatform
HasSEP
HWModelStr
Image4Supported
InternalBuild
InverseDeviceID
ipad
MixAndMatchPrevention
MLBSerialNumber
MobileSubscriberCountryCode
MobileSubscriberNetworkCode
ModelNumber
PartitionType
PasswordProtected
ProductName
ProductType
ProductVersion
ProximitySensorCalibrationDictionary
RearFacingCameraHFRCapability
RegionCode
RegionInfo
SDIOManufacturerTuple
SDIOProductInfo
SerialNumber
SIMTrayStatus
SoftwareBehavior
SoftwareBundleVersion
SupportedDeviceFamilies
SupportedKeyboards
telephony
UniqueChipID
UniqueDeviceID
UserAssignedDeviceName
wifi
WifiVendor
As others have noted, no, there is no official way of getting this information. Apple clearly knows (look in iTunes when you sync), so they could make it available. Might be worth raising a Radar.

Resources