Watchmaker Wiki

The ultimate watch maker for Android Wear!

User Tools

Site Tools


tips:dynamiccolor

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revisionBoth sides next revision
tips:dynamiccolor [2015/01/22 02:43] – [Introduction] rahul_pawatips:dynamiccolor [2015/01/22 03:02] – [Transition Color As Battery Drains, From Green to Yellow to Red] rahul_pawa
Line 50: Line 50:
  
  
-==== Transition Color As Battery Drains, From Green to Yellow to Red ====+==== Gradually Transition Color As Battery Drains, From Green to Yellow to Red ====
  
  
Line 57: Line 57:
 Here again we are using a condition to set the color value of an object, this time the condition is {bl} > 66, and the value we get is a function of the battery level: string.format('%x%s',(100-{bl})*255/33,'FF00'). I will explain what that means, but the idea is the amount of red increases as the battery lowers from 100 to 66, effectively transitioning the color from a bright green to a bright yellow. Here again we are using a condition to set the color value of an object, this time the condition is {bl} > 66, and the value we get is a function of the battery level: string.format('%x%s',(100-{bl})*255/33,'FF00'). I will explain what that means, but the idea is the amount of red increases as the battery lowers from 100 to 66, effectively transitioning the color from a bright green to a bright yellow.
  
-The string.format function is how you build a string in LUA. The part in the parenthesis after string.format are its arguments, each argument is separated by a comma. So the first argument is '%x%s', second is (100-{bl})*255/33 and third is 'FF00'.+The string.format function is how you build a string in LUA. I am using it here to convert the decimal number generated by the formula (100-{bl})*255/33 into a hexadecimal number that is part of a 6 digit string that is the color code I want displayed. The part in the parenthesis after string.format are the function arguments, each argument is separated by a comma. So the first argument is '%x%s', second is (100-{bl})*255/33 and third is 'FF00'.
  
-Let's talk about that first argument, you always need something like this when you use string.format. It always needs the single quotes (''), and in those quotes is a code that tells LUA what our string will look like. In this case, %x tells it the first thing in our string will be a hexadecimal number, and %s tells us it will be followed by a string (some letters or numbers). The formatting here is also important, note that there is no space between %x and %s, this tells LUA that there shouldn't be a space between the hexadecimal number and the string.+Let's talk about that first argument, you always need something like this when you use string.format. It always needs the single quotes (' '), and in those quotes is a code that tells LUA what our string will look like. In this case, %x tells it the first thing in our string will be a hexadecimal number, and %s tells us it will be followed by a string (some letters or numbers). The formatting here is also important, note that there is no space between %x and %s, this tells LUA that there shouldn't be a space between the hexadecimal number and the string.
  
 The second argument is a bit of code that uses the battery level, it correlates to the %x in the first argument. The code itself is: (100-{bl})*255/33. I'm using this code to set the red value of the color, I want it to be 0 when the battery is at 100, and 255 when the battery is at 67. Since this code only works when the battery is greater than 66, the code 100-{bl} will give us a number between 0 and 33. I then multiply by 255 and divide by 33 to change that range of 0 to 33 into 0 to 255. Simply putting %x in the first argument does the hard work of converting the result of this code into a hexadecimal number. The second argument is a bit of code that uses the battery level, it correlates to the %x in the first argument. The code itself is: (100-{bl})*255/33. I'm using this code to set the red value of the color, I want it to be 0 when the battery is at 100, and 255 when the battery is at 67. Since this code only works when the battery is greater than 66, the code 100-{bl} will give us a number between 0 and 33. I then multiply by 255 and divide by 33 to change that range of 0 to 33 into 0 to 255. Simply putting %x in the first argument does the hard work of converting the result of this code into a hexadecimal number.
tips/dynamiccolor.txt · Last modified: 2016/07/05 19:32 by wmissimer