Skip to content

Instantly share code, notes, and snippets.

@Gperez88
Last active December 9, 2020 21:19
Show Gist options
  • Save Gperez88/1f094c13b5c6b119357bb3d840f380fd to your computer and use it in GitHub Desktop.
Save Gperez88/1f094c13b5c6b119357bb3d840f380fd to your computer and use it in GitHub Desktop.
RGB to Hex Color
void main() {
final colorConverter = ColorConverter();
final hexColor = colorConverter.rgbToHex([255,255,255]);
print(hexColor);
}
class ColorConverter {
String rgbToHex(List<int> rgb) {
if (!_isValidRGB(rgb))
return "El RGB no es valido, debe ser un arreglo de 3 elementos, ejem: [255,255,255]";
final red = _intToHex(rgb[0]);
final green = _intToHex(rgb[1]);
final blue = _intToHex(rgb[2]);
return '#$red$green$blue'.toUpperCase();
}
bool _isValidRGB(List<int> rgb) => rgb.length == 3;
String _intToHex(int value) {
var hex = value.toRadixString(16);
if (hex.length < 2) hex = '0$hex';
return hex;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment