How to get RGB value of top color? Created 4 weeks ago2022-07-16 14:09:26 UTC by JacksBestGaming JacksBestGaming

Created 4 weeks ago2022-07-16 14:09:26 UTC by JacksBestGaming JacksBestGaming

Posted 4 weeks ago2022-07-16 14:09:26 UTC Post #346713
I am making a coop mod so I can play Half-Life in coop without the 4k models and improved engine of Sven Co-op. I want to set the UI color to the top color of the player, but I am not smart. I have no clue where to start, so any help would be appreciated.
Posted 4 weeks ago2022-07-16 14:34:19 UTC Post #346714
You will have to manually modify the files ClientScheme.res and GameMenu.res. I have no clue how to do that, sorry. But if you want to change the RGB values you will be fine.
Posted 4 weeks ago2022-07-16 16:26:10 UTC Post #346715
By UI do you mean the in-game HUD, or the main menu, or menus like the green "New Game", "Create Game", "Load", or "Options" menus?
Posted 4 weeks ago2022-07-16 18:08:32 UTC Post #346716
The in-game HUD, like the health and armor.
Posted 4 weeks ago2022-07-16 18:49:11 UTC Post #346718
You'll need a custom DLL. Know any programming? If you do, check out Half-Life Programming - Getting Started and then Tutorial: Customising the HUD colour (ignore the part "For this tutorial you will need" of that tutorial, you don't need those specific versions)
Posted 4 weeks ago2022-07-17 03:17:02 UTC Post #346725
Do you know what I need to program to get the RGB of the top color?
Is there a variable that gives this value, or do I need to program something to get the RGB value from a palette number?
Posted 4 weeks ago2022-07-17 15:17:42 UTC Post #346733
Do you mean the one you choose with the top slider when you choose player model?
User posted image
I think this should give you that color. Call getLocalPlayerTopColorRgb(), you'll find the red in the return value's [0], green in [1], and blue in [2]. I have not tested any of this code. But it works in my head :D

#include "cl_dll.h"
#include "com_model.h"
#include "r_studioint.h"

#include <array>

extern engine_studio_api_t IEngineStudio;

typedef std::array<unsigned char, 3> rgb_color;

// Converts hue (in degrees) to 8bpc linear RGB, with 100% saturation and brightness
constexpr rgb_color hueToRgb(int hue) {
	rgb_color result{};

	if (hue < 0 || hue >= 360) { // Out of range, make it red
		hue = 0;
	}
	
	const int cycleProgress = hue % 120;
	const int channelA = hue / 120;
	const int channelB = (channelA + 1) % result.size();
	if (cycleProgress < 60) {
		// If hue is between   0 and  59 inclusive, channelA is 0. The result is between #FF0000 and #FFFA00 inclusive
		// If hue is between 120 and 179 inclusive, channelA is 1. The result is between #00FF00 and #00FFFA inclusive
		// If hue is between 240 and 299 inclusive, channelA is 2. The result is between #0000FF and #FA00FF inclusive
		result[channelA] = 255;
		result[channelB] = (unsigned char) ((cycleProgress * 255) / 60);
	} else {
		// If hue is between  60 and 119 inclusive, channelA is 0. The result is between #FFFF00 and #04FF00 inclusive
		// If hue is between 180 and 239 inclusive, channelA is 1. The result is between #00FFFF and #0004FF inclusive
		// If hue is between 300 and 359 inclusive, channelA is 2. The result is between #FF00FF and #FF0004 inclusive
		result[channelA] = (unsigned char) (((120 - cycleProgress) * 255) / 60);
		result[channelB] = 255;
	}
	return result;
}

rgb_color getPlayerTopColorRgb(int playerIndex) {
	rgb_color result { 255, 255, 255 };

	if (playerIndex >= 0 && playerIndex < gEngfuncs.GetMaxClients())
		const player_info_t * const playerInfo = IEngineStudio.PlayerInfo(playerIndex);
		if (playerInfo) {
			const int topColorHue = playerInfo->topcolor;
			result = hueToRgb(topColorHue);
		}
	}

	return result;
}

rgb_color getLocalPlayerTopColorRgb() {
	rgb_color result { 255, 255, 255 };

	const cl_entity_t * const localPlayer = gEngfuncs.GetLocalPlayer();
	if (localPlayer) {
		const int playerEntityIndex = localPlayer->index;
		const int playerIndex = playerEntityIndex - 1;
		result = getPlayerTopColorRgb(playerIndex);
	}

	return result;
}

Posted 1 week ago2022-08-07 12:05:20 UTC Post #346765
Did this work for you, Jack?
Posted 2 days ago2022-08-13 03:40:27 UTC Post #346777
I haven't tried it yet, but I am getting back into coding my mod so I will try it right now. Thanks for the help!
You must be logged in to post a response.