Sign in to follow this  
xForcer

[Tutorial/Guide] Custom Chat HEX Colors (C#) - 0.3.7!

Recommended Posts

 

Hello everyone!

This will be a short guide on how to use colors when using custom chat, not the default one.
I know I am not the only one frustrated about this so that's why I want to share this to everyone.

 

Important credits:

- Carlos (ex RageMP member)
  Helped me the most, basically solving everything and giving me a big smile. I still owe you a beer :D 
@Chainksain
  This dude actually has the same way of doing things as me so big shoutout to him for examples

 

Let's start!

 

Requirements:

Visual Studio 17 or better
- Net Core 2.2
- RageMP C# 0.3.7
 

 

1. First, create a class that will be used for your chat colorization stuff. Everything will be commented

using System;

namespace CustomChatColorsTutorial
{
    public static class ChatColorization
    {
        // Few general colors
        public const string ColorRed = "#A80707";
        public const string ColorDodgerBlue = "#1E90FF";
      
        /// <summary>
        /// HEX colorization
        /// </summary>
        /// <param name="text">The text you want to colorize</param>
        /// <param name="color">Use colors from ChatColorization class</param>
        /// <returns></returns>
        public static string ColorizeText(string text, string color)
        {
            return "<strong style='color:" + color + "'>" + text + "</strong>";
        }
    }
}

 

2. To use colors, do something like this: 

using System;
using GTANetworkAPI;
using GTANetworkMethods;

namespace CustomChatColorsTutorial
{
    public static class Chat
    {        
        public void SendExampleMessage(Client player)
        {                     
             NAPI.Chat.SendChatMessageToPlayer(player, ChatColorization.ColorizeText("This is an example", ChatColorization.ColorDodgerBlue));            
        }
    }
}

 

This will output:  This is an example

 

3. This was simple so far, I agree. But what about mixing few colors in one sentence? Let's say we want to send an error message but nice and clean. This is how we do it:

using System;

namespace CustomChatColorsTutorial
{
    public static class ChatColorization
    {
        // Few general colors
        public const string ColorRed = "#A80707";
        public const string ColorDodgerBlue = "#1E90FF";

        // Remember: If you don't explicitly colorize text, it will use your custom chat's default color (white)
        // In this case, brackets will be white
        public static string ErrorMessage(string message)
        {
            return "(" + ColorizeText("Error", ColorRed) + ") - " + message;
        }
      
        /// <summary>
        /// HEX colorization
        /// </summary>
        /// <param name="text">The text you want to colorize</param>
        /// <param name="color">Use colors from ChatColorization class</param>
        /// <returns></returns>
        public static string ColorizeText(string text, string color)
        {
            return "<strong style='color:" + color + "'>" + text + "</strong>";
        }
    }
}

 

And then we use that in our Chat class

using System;
using GTANetworkAPI;
using GTANetworkMethods;

namespace CustomChatColorsTutorial
{
    public static class Chat
    {        
        public void SendExampleMessage(Client player)
        {                     
             NAPI.Chat.SendChatMessageToPlayer(player, ChatColorization.ErrorMessage("Something bad happened, please contact the adminstrator!"));       
        }
    }
}

 

This will output: (Error) - Something bad happened, please contact the adminstrator!

 

That's it!
You can now use custom colors in your custom chat. 
If you need assistance, just ask.
If something is wrong, please tell me to correct it.
Leave feedback.


Thank you for reading!
 

xForcer

 

Changelog: 

  • Cleaner formatting of ErrorMessage. Removed substring constants
Edited by xForcer

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.