[Tutorial] Creating a rainbow textdraw
#1

Hello, some time ago i made this simple script and i want to share with you.
This script allows you to create a textdraw that changes its color smoothly after some time.
Something similar with this image: https://www.youtube.com/watch?v=w6SA7bEkLsQ

So how we are gonna do that?
Colors can be written in different formats (rgb, Hex, RGBA)
The most simple is rgb (stands for red, green, blue), witch stores 3 numbers, reprezenting the amount of pigment a color has from 0 to 255.
For example, rgb(255, 0, 0) is pure red. rgb(0, 0, 255) is pure blue. We also can combine colors, so rgb(255, 0, 255) is purple.

If we search "color picker", we will get directly a color picker that looks like this:


The left side shows the value of the color in rgb - in this case, rgb(255, 0, 0) and hex - in this case, #ff0000.
If we slide with the down thin bar, we can get colors, also, the rgb values are changing.

If we start from the pure red (left) rgb(255,0,0) and slide to yellow, we can see that the green amount of color is increasing from 0 to 255, so the purest yellow is rgb(255, 255, 0). This stands for all the colors, the amount of pigments are increasing and decreasing.

Since we know that, let's put it in a pawn script:

First, we create variables for red, green and blue (the main colors in this case) and the textdraw.
PHP Code:
#include <a_samp>
new redgreenblue;
new 
PlayerText:MyTextdraw[MAX_PLAYERS]; 
As we have seen, the colors starts from rgb(255, 0, 0), so we insert into OnGameModeInit the certain values. We also need a timer that repeats every second.

PHP Code:
forward UpdateColor();
public 
UpdateColor(){
   
//will complete this later
}
public 
OnGameModeInit()
{
   
red 255;
   
green 0;
   
blue 0;
   
SetTimer("UpdateColor"1000true);
   return 
1;
}
//creating the textdraw:
public OnPlayerConnect(playerid){
        
MyTextdraw[playerid] = CreatePlayerTextDraw(playerid637.980529424.166625"This is a textdraw!");
        
PlayerTextDrawLetterSize(playeridMyTextdraw[playerid], 0.3087051.541666);
        
PlayerTextDrawAlignment(playeridMyTextdraw[playerid], 3);
        
PlayerTextDrawColor(playeridMyTextdraw[playerid], -1);
        
PlayerTextDrawSetShadow(playeridMyTextdraw[playerid], 0);
        
PlayerTextDrawSetOutline(playeridMyTextdraw[playerid], 1);
        
PlayerTextDrawBackgroundColor(playeridMyTextdraw[playerid], 255);
        
PlayerTextDrawFont(playeridMyTextdraw[playerid], 1);
        
PlayerTextDrawSetProportional(playeridMyTextdraw[playerid], 1);
        
PlayerTextDrawSetShadow(playeridMyTextdraw[playerid], 0);
        return 
1;

So at the start of the gamemode, the color is pure red - rgb(255, 0, 0);
Every second, our color will change. I recomend you changing it by 15 every time, i tested it and is smooth enough.
Returing to UpdateColor():

PHP Code:
public UpdateColor(){
   if(
red == 255 && blue == && green != 255green += 15//as i said before, the green increases until is 255 - rgb(255, 255, 0) that is yellow
   
if(red != && blue == && green == 255red -= 15//we continue decreasing the red, so at the end we will have rgb(0, 255, 0) - pure green
   //and so on.. :
   
if(red == && blue != 255 && green == 255blue += 15//making it cyan
   
if(red == && blue == 255 && green != 0green -= 15;  //making it pure blue
   
if(red != 255 && blue == 255 && green == 0red += 15//making it purple
   
if(red == 255 && green == && blue != 0blue -= 15//making it pure red and reinitializing the cycle
  //we have all the colors needed in rbg format (r, g, b), but textdraws use HEX format (0xRRGGBBAA) where AA is the alpha. So we use the folowing script:
   
new finalcolorhex//the color that will be applied on the textdraw
   
finalcolorhex red<<24 green<<16 blue<<255// where 255 is the alpha, you can modify this value to change its transparency.
   //we now have to change it for all players:
   
for(new iMAX_PLAYERSi++){
       
PlayerTextDrawColor(iMyTextdraw[i], finalcolorhex); //updating the color
       
PlayerTextDrawShow(iMyTextdraw[i]); //acording to samp wiki, we have to reshow the textdraw to the player in order to update
   
}

And that's it, you have a rainbow textdraw
Sorry for gramatical issues Ї\_(ツ)_/Ї
Reply
#2

good job
Reply
#3

Thanks <3
Reply
#4

thanks alot
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)