Skip to content

Commit

Permalink
Reveal and document MakeColorHSB
Browse files Browse the repository at this point in the history
These "secret" functions are very useful and shouldn't be kept hidden.  Let's start documenting them.
  • Loading branch information
npiegdon committed Dec 19, 2021
1 parent e6b8c6e commit d51f9a4
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 12 deletions.
12 changes: 3 additions & 9 deletions drawing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -311,12 +311,6 @@ void SaveImage(unsigned int suffix)
//


// Creates a color using a (hue, saturation, value) triple instead of (red, green, blue).
//
// - S and V are in the usual [0, 256) range, but H is in degrees: [0, 360)
//
Color MakeColorHSV(int h, int s, int v);

// If your program is going to be writing every pixel every frame, this is a simple optimization
// over issuing Width*Height calls to DrawPixel. Instead of spending time on the thread-safety
// overhead Width*Height times, the entire operation is performed in bulk behind a single lock.
Expand Down Expand Up @@ -391,12 +385,12 @@ Color MakeColor(int r, int g, int b, int a)

constexpr Color MakeColor(int r, int g, int b)
{
return (0xFF << 24) | ((r & 0xFF) << 16) | ((g & 0xFF) << 8) | ((b & 0xFF) << 0);
return 0xFF000000 | ((r & 0xFF) << 16) | ((g & 0xFF) << 8) | ((b & 0xFF) << 0);
}

Color MakeColorHSV(int hue, int sat, int val)
Color MakeColorHSB(int hue, int sat, int val)
{
float h = fmod(hue / 360.0f, 1.0f);
float h = min(1.0f, max(0.0f, (hue % 360) / 360.0f));
float s = min(1.0f, max(0.0f, sat / 255.0f));
float v = min(1.0f, max(0.0f, val / 255.0f));

Expand Down
16 changes: 16 additions & 0 deletions drawing.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,22 @@ static const Color LightMagenta = MakeColor(255, 85, 255);
static const Color Yellow = MakeColor(255, 255, 85);
static const Color White = MakeColor(255, 255, 255);

// MakeColorHSB is like MakeColor but instead of using red, green, and blue
// values, you get to specify the hue. This is ideal for making rainbows!
//
// hue is an angle (in degrees) between 0 and 360.
// (0 is red, 30 is orange, 60 is yellow, 240 is blue...)
//
// saturation and brightness should be between 0 and 255.
//
// saturation=0 makes everything grayscale.
// saturation=255 will be the most vivid version of that color.
//
// brightness=0 is always black.
// brightness=255 is the brightest version of that color.
//
Color MakeColorHSB(int hue, int saturation, int brightness);




Expand Down
5 changes: 2 additions & 3 deletions example8_smoke.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,6 @@ void velocityStep(vector<float> &u, vector<float> &v, vector<float> &u0, vector<


// Secret functions from drawing.cpp!
Color MakeColorHSV(int h, int s, int v);
void Present(const vector<Color> &screen);
void DrawString(int x, int y, const string &s, const Color c, bool centered, function<void(int x, int y, Color c)> customDraw);

Expand All @@ -177,11 +176,11 @@ Color FluidColor(float u, float v, float density, bool showVelocity)
{
int h = min(360, max(0, int(sqrt(u*u + v*v) * 1500.0)));
int v = min(255, max(0, int(density * 500.0)));
return MakeColorHSV(h, 255, v);
return MakeColorHSB(h, 255, v);
}

const int value = min(360, max(0, int(density * 100.0f)));
return MakeColorHSV(max(0, value - 310), value / 2, value);
return MakeColorHSB(max(0, value - 310), value / 2, value);
}

// Returns a list of points between the given coordinates
Expand Down

0 comments on commit d51f9a4

Please sign in to comment.