B4R Question Is there a StringBuilder library for B4R?

Robert Gately

Member
Licensed User
What do you need it for?

I was looking at this example: Utilizzo di StringBuilder ? ...and it looked like they were using a StringBuilder library.

I was looking for a way to convert 256 numbers into RGB colors. Not sure that I need StringBuilder any more, since I decided to make a simple 24 color lookup table.

Also, I was hoping to store the lookup table in ProgMem but couldn't get it to save RGB hex numbers.
 
Last edited:
Upvote 0

miker2069

Active Member
Licensed User
Longtime User
Are you looking to convert a number array to a string (i.e. Array of Int(100,200,300)) to "100,200,300"? What's it need to look like.
 
Upvote 0

Robert Gately

Member
Licensed User
Are you looking to convert a number array to a string (i.e. Array of Int(100,200,300)) to "100,200,300"? What's it need to look like.
Strings aren't that important to me after all. I was looking at some example string code that looked helpful.

Ideally, I'd like a routine that can convert a byte into 256 RGB values. It can be decimal: 255,255,255 or it can be hex: 0xffffff.

I'm working with a strip of 60 WS2812b LEDs. The LEDs expect the RGB format, like:"setled(ledNum,125,250,0). But the sensor data is in bytes and the 256 values need to be converted to spread evenly across the much larger range of RGB values.

Since I wasn't getting anywhere with conversion, I made a Select/Case routine that maps the 256 byte values to 24 colors in the RGB format. It is probably all the colors I need for my particular color scheme (Red, Magenta, Blue, Cyan, Green, Yellow and 3 colors in between them).

I'd be happy to use hex values (0xffffff) and convert them into RGB format, especially if they could be saved to ProgMem. I couldn't figure out how to do it with those large hex values.

If nothing better comes along, I'm happy with the 24 color look-up table. If you have something that you think might be helpful, please share. Otherwise, I'm happy to proceed with what I've got.

Thanks
 
Upvote 0

Robert Gately

Member
Licensed User
Avoid using strings if you can.

You can store RGB values in long variables or as arrays of three bytes. Use GlobalStore or Queue or Stack to store those arrays.
[module] GlobalStore - Global objects storage
Thanks for the help! When you say "arrays of three bytes", do you mean 3 dimensional array?

I'm also trying to decipher this Arduino code that I used a few years ago...

B4X:
// Input a value of 0 to 255 to get a color value. The colors are a transition r - g - b - back to r.
uint32_t Wheel(byte WheelPos) {
  //if (debugPrint==1) {Serial.println("Wheel");}
  if(WheelPos < 85) {
   return strip1.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
  } else if(WheelPos < 170) {
   WheelPos -= 85;
   return strip1.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  } else {
   WheelPos -= 170;
   return strip1.Color(0, WheelPos * 3, 255 - WheelPos * 3);
  }
}

Sorry if I'm off topic (strings) now.
 
Upvote 0

Robert Gately

Member
Licensed User
I was able to rewrite the Arduino code and it works perfectly. 0 - 255 produces a smooth rainbow transition through all of the colors. I don't need a look-up table anymore.

Erel, would you like me to polish this up and post it in the code snippet section?

B4X:
Private Sub ByteToRGB (Value As Byte) As ULong
      If Value< 85 Then
          Log(Value * 3, 255 - Value* 3, 0)
          Strip1.setColor(10, Value * 3, 255 - Value* 3, 0)
      Else If Value< 170 Then
          Value= Value-85
          Log(255 - Value* 3, 0, Value* 3)
          Strip1.setColor(10, 255 - Value* 3, 0, Value* 3)
      Else
          Value= Value- 170
          Log(0, Value* 3, 255 - Value* 3)
          Strip1.setColor(10, 0, Value* 3, 255 - Value* 3)
      End If
      Strip1.show
End Sub

Also, I'd like to delete this thread regarding StringBuilder, since it turned out to be why I thought I needed StringBuilder. It would just be misleading and annoying for anyone who was looking for information about StringBuilder.
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Also, I'd like to delete this thread regarding StringBuilder, since it turned out to be why I thought I needed StringBuilder. It would just be misleading and annoying for anyone who was looking for information about StringBuilder.
It is important discussion. I prefer not to delete it.

Erel, would you like me to polish this up and post it in the code snippet section?
Sure. That will be useful.
 
Upvote 0
Top