Last active
April 28, 2023 12:15
-
-
Save Journeyman1337/0439b9ffa339d1bbd8c70cb2521e0da8 to your computer and use it in GitHub Desktop.
simple rgba float color struct in cpp with conversions and from uint32_t hex codes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Copyright 2020 Daniel Valcour | |
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
*/ | |
#include "fcolor.h" | |
fcolor::fcolor() { | |
this->r = 1.0f; | |
this->g = 1.0f; | |
this->b = 1.0f; | |
this->a = 1.0f; | |
} | |
fcolor::fcolor(uint32_t hex_code) { | |
this->r = (float)((hex_code & 0xFF000000) >> 24) / 255.0f; | |
this->g = (float)((hex_code & 0x00FF0000) >> 16) / 255.0f; | |
this->b = (float)((hex_code & 0x0000FF00) >> 8) / 255.0f; | |
this->a = (float)((hex_code & 0x000000FF) >> 0) / 255.0f; | |
} | |
fcolor::fcolor(float r, float g, float b, float a) { | |
this->r = r; | |
this->g = g; | |
this->b = b; | |
this->a = a; | |
} | |
uint32_t fcolor::to_hex() { | |
return ((uint32_t)((((this->r > 1.0f) ? (uint8_t)255 : (this->r < 0.0f) ? (uint8_t)0 : (uint8_t)(this->r * 255.0f)) << 24)) + | |
((uint32_t)(((this->g > 1.0f) ? (uint8_t)255 : (this->g < 0.0f) ? (uint8_t)0 : (uint8_t)(this->g * 255.0f)) << 16)) + | |
((uint32_t)(((this->b > 1.0f) ? (uint8_t)255 : (this->b < 0.0f) ? (uint8_t)0 : (uint8_t)(this->b * 255.0f)) << 8)) + | |
((uint32_t)(((this->a > 1.0f) ? (uint8_t)255 : (this->a < 0.0f) ? (uint8_t)0 : (uint8_t)(this->a * 255.0f)) << 0))); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Copyright 2020 Daniel Valcour | |
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
*/ | |
#pragma once | |
#include <stdint.h> | |
//A struct with four floats that represent the r, g, b, and a values of a color between 0 and 1. | |
struct fcolor | |
{ | |
//The red color value. | |
float r; | |
//The green color value. | |
float g; | |
//The blue color value. | |
float b; | |
//The alpha/transparency value. | |
float a; | |
//Create a color with default values (opaque white). | |
fcolor(); | |
//Create a color from an unsigned 32 bit int hex color. | |
fcolor(uint32_t hex_color); | |
//Create a color from four float color parameters. | |
fcolor(float r, float g, float b, float a); | |
//Return this color as an unsigned 32 bit int hex code (accuracy is limited). | |
uint32_t to_hex(); | |
}; | |
//Predefined jcolor consts | |
//Predefined white color const (red = 1.0, green = 1.0, blue = 1.0, alpha = 1.0). | |
const fcolor FC_WHITE = { 1.0f, 1.0f, 1.0f, 1.0f }; | |
//Predefined silver color const (red = 0.75, green = 0.75, blue = 0.75, alpha = 1.0). | |
const fcolor FC_SILVER = { 0.75f, 0.75f, 0.75f, 1.0f }; | |
//Predefined gray/grey color const (red = 0.5, green = 0.5, blue = 0.5, alpha = 1.0). | |
const fcolor FC_GRAY = {0.5f, 0.5f, 0.5f, 1.0f}; | |
//Predefined black color const (red = 0.0, green = 0.0, blue = 0.0, alpha = 1.0). | |
const fcolor FC_BLACK = {0.0f, 0.0f, 0.0f, 1.0f}; | |
//Predefined red color const (red = 1.0, green = 0.0, blue = 0.0, alpha = 1.0). | |
const fcolor FC_RED = {1.0f, 0.0f, 0.0f, 1.0f}; | |
//Predefined maroon/dark red color const (red = 0.5, green = 0.0, blue = 0.0, alpha = 1.0). | |
const fcolor FC_MAROON = {0.5f, 0.0f, 0.0f, 1.0f}; | |
//Predefined yellow color const (red = 1.0, green = 1.0, blue = 0.0, alpha = 1.0). | |
const fcolor FC_YELLOW = {1.0f, 1.0f, 0.0f, 1.0f}; | |
//Predefined olive color const (red = 0.5, green = 0.5, blue = 0.0, alpha = 1.0). | |
const fcolor FC_OLIVE = {0.5f, 0.5f, 0.0f, 1.0f}; | |
//Predefined lime color const (red = 0.0, green = 1.0, blue = 0.0, alpha = 1.0). | |
const fcolor FC_LIME = {0.0f, 1.0f, 0.0f, 1.0f}; | |
//Predefined green color const (red = 0.0, green = 0.5, blue = 0.0, alpha = 1.0). | |
const fcolor FC_GREEN = {0.0f, 0.5f, 0.0f, 1.0f}; | |
//Predefined aqua color const (red = 0.0, green = 1.0, blue = 1.0, alpha = 1.0). | |
const fcolor FC_AQUA = {0.0f, 1.0f, 1.0f, 1.0f}; | |
//Predefined teal color const (red = 0.0, green = 0.5, blue = 0.5, alpha = 1.0). | |
const fcolor FC_TEAL = {0.0f, 0.5f, 0.5f, 1.0f}; | |
//Predefined blue color const (red = 0.0, green = 0.0, blue = 1.0, alpha = 1.0). | |
const fcolor FC_BLUE = { 0.0f, 0.0f, 1.0f, 1.0f }; | |
//Predefined navy/dark blue color const (red = 0.0, green = 0.0, blue = 0.5, alpha = 1.0). | |
const fcolor FC_NAVY = {0.0f, 0.0f, 0.5f, 1.0f}; | |
//Predefined fuschia/pink color const (red = 1.0, green = 0.0, blue = 1.0, alpha = 1.0). | |
const fcolor FC_FUSCHIA = {1.0f, 0.0f, 1.0f, 1.0f}; | |
//Predefined puple color const (red = 0.5, green = 0.0, blue = 0.5, alpha = 1.0). | |
const fcolor FC_PURPLE = {0.5f, 0.0f, 0.5f, 1.0f}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment