Skip to content

Instantly share code, notes, and snippets.

@sylveon
Created June 9, 2018 20:41
Show Gist options
  • Save sylveon/b63621297d566d06ab38bd5586eca0ed to your computer and use it in GitHub Desktop.
Save sylveon/b63621297d566d06ab38bd5586eca0ed to your computer and use it in GitHub Desktop.
VSCode C++ bug
#include <array>
#include <d2d1.h>
#include <iostream>
#include "SColour.hpp"
constexpr uint8_t HueGradientPrecision = 20; // Number of steps in the hue gradient
constexpr const std::array<D2D1_GRADIENT_STOP, HueGradientPrecision> CalculateHueGradient()
{
std::array<D2D1_GRADIENT_STOP, HueGradientPrecision> gradientStops{};
SColour tempcol{};
tempcol.h = 359;
tempcol.s = 100;
tempcol.v = 100;
tempcol.UpdateRGB();
const float step = 359 / (float)HueGradientPrecision;
for (uint8_t i = 0; i < HueGradientPrecision; i++)
{
// Due to some weird MSBuild thing, C++17 is half implemented in Clang-CL.
// Because of that, we don't have constexpr operator[] on std::array.
// Access the raw underlying array directly instead.
gradientStops._Elems[i] = {
i / (float)(HueGradientPrecision - 1),
{
tempcol.r / 255.0f,
tempcol.g / 255.0f,
tempcol.b / 255.0f,
1.0f
}
};
// Clang errors out on the use of -= here
tempcol.h = tempcol.h - step;
tempcol.UpdateRGB();
}
return gradientStops;
}
inline const auto &GetHueGradient()
{
static constexpr auto value = CalculateHueGradient();
return value;
}
int main()
{
std::cout << GetHueGradient()[10].position;
}
#pragma once
#include <algorithm>
#include <cstdint>
struct SColour {
// Red, green and blue
uint8_t r, g, b;
// Hue, saturation and value
unsigned short h;
uint8_t s, v;
// Alpha
uint8_t a;
#pragma warning(push)
#pragma warning(disable: 4244)
// Updates RGB from HSV
constexpr void UpdateRGB()
{
const float val = v / 100.0f;
if (s == 0) // Acromatic color (gray). Hue doesn't mind.
{
r = b = g = 255.0f * val;
return;
}
const float base = 255.0f * (1.0f - (s / 100.0f)) * val;
switch (h / 60)
{
case 0:
r = 255.0f * val;
g = (255.0f * val - base) * (h / 60.0f) + base;
b = base;
break;
case 1:
r = (255.0f * val - base) * (1.0f - ((h % 60) / 60.0f)) + base;
g = 255.0f * val;
b = base;
break;
case 2:
r = base;
g = 255.0f * val;
b = (255.0f * val - base) * ((h % 60) / 60.0f) + base;
break;
case 3:
r = base;
g = (255.0f * val - base) * (1.0f - ((h % 60) / 60.0f)) + base;
b = 255.0f * val;
break;
case 4:
r = (255.0f * val - base) * ((h % 60) / 60.0f) + base;
g = base;
b = 255.0f * val;
break;
case 5:
r = 255.0f * val;
g = base;
b = (255.0f * val - base) * (1.0f - ((h % 60) / 60.0f)) + base;
break;
}
}
// Updates HSV from RGB
constexpr void UpdateHSV()
{
const uint8_t &max = (std::max)((std::max)(r, g), b);
const uint8_t &min = (std::min)((std::min)(r, g), b);
const float delta = max - min;
if (max == 0)
{
s = h = v = 0;
return;
}
v = max / 255.0f * 100.0f;
s = delta / (float)max * 100.0f;
short temp = 0;
if (r == max)
{
temp = 60 * ((g - b) / delta);
}
else if (g == max)
{
temp = 60 * (2 + (b - r) / delta);
}
else
{
temp = 60 * (4 + (r - g) / delta);
}
if (temp < 0)
{
h = temp + 360;
}
else
{
h = temp;
}
}
#pragma warning(pop)
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment