Jump to content

How to get other colors than the normal ones.


epik666

Recommended Posts

So i was wondering how to use other colors than the on listed in the "TextFormatting" file. 
Example

mc.fontRenderer.drawStringWithShadow(Mod.MODNAME + " " + TextFormating.GREEN + Mod.MODVER,1,1,Mod.MYHEXCOLOR);

How would i go on using brown as example?

Link to comment
Share on other sites

I use the following class to get colors outside the predefined ones, it should be able to fit your case I belive.


To understand how it works, it is simply that RGBA colors are encoded in a single integer, where each set of 2 bytes store a single color.

2 bytes can encode the values 0-255.Which is the reason that RGB is defined in that range.
 

public class GuiHelper {
    private GuiHelper() {

    }

    public static int RGBAToInt(int R, int G, int B, int A) {
        // Make sure the values are between 0 and 255
        R &= 255;
        G &= 255;
        B &= 255;
        A &= 255;

        // Convert
        return A << 24 | R << 16 | G << 8 | B;
    }

    public static int RGBAToInt(int RGB, int A) {
        return RGBAToInt(RGB, RGB, RGB, A);
    }

    public static int RGBAToInt(int RGB) {
        return RGBAToInt(RGB, RGB, RGB, 255);
    }

    public static int RGBAToInt(int R, int G, int B) {
        return RGBAToInt(R, G, B, 255);
    }

}

 

Link to comment
Share on other sites

3 minutes ago, [DK] Headcrab [DK] said:

I use the following class to get colors outside the predefined ones, it should be able to fit your case I belive.


To understand how it works, it is simply that RGBA colors are encoded in a single integer, where each set of 2 bytes store a single color.

2 bytes can encode the values 0-255.Which is the reason that RGB is defined in that range.
 


public class GuiHelper {
    private GuiHelper() {

    }

    public static int RGBAToInt(int R, int G, int B, int A) {
        // Make sure the values are between 0 and 255
        R &= 255;
        G &= 255;
        B &= 255;
        A &= 255;

        // Convert
        return A << 24 | R << 16 | G << 8 | B;
    }

    public static int RGBAToInt(int RGB, int A) {
        return RGBAToInt(RGB, RGB, RGB, A);
    }

    public static int RGBAToInt(int RGB) {
        return RGBAToInt(RGB, RGB, RGB, 255);
    }

    public static int RGBAToInt(int R, int G, int B) {
        return RGBAToInt(R, G, B, 255);
    }

}

 

How would i use it? I got the class in my util folder and now idk how to use it :P

Link to comment
Share on other sites

 

4 minutes ago, epik666 said:

How would i use it? I got the class in my util folder and now idk how to use it :P

The last argument for FontRenderer#drawString is the color as an integer.

 

So for example:

fontRenderer.drawString("My string to draw in RED", stringPosX, stringPosY, GuiHelper.RGBAToInt(255, 0, 0));

 

Same applies for FontRenderer#drawStringWithShadow

Edited by [DK] Headcrab [DK]
Link to comment
Share on other sites

24 minutes ago, [DK] Headcrab [DK] said:

 

The last argument for FontRenderer#drawString is the color as an integer.

 

So for example:


fontRenderer.drawString("My string to draw in RED", stringPosX, stringPosY, GuiHelper.RGBAToInt(255, 0, 0));

 

Same applies for FontRenderer#drawStringWithShadow

Thanks, it seems like it does not work outside fontrenders. Im trying to use it in something else and i can't seem to figure out how to do it.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.