ColorHSLx<float, Colorf>::ColorHSLx(const Colorf &color) { float red = color.get_red(); float green = color.get_green(); float blue = color.get_blue(); const float min_value = min(red, min(blue, green)); const float max_value = max(red, max(blue, green)); a = color.get_alpha(); l = (max_value + min_value) / 2.0f; if (min_value == max_value) { h = 0.0f; s = 0.0f; return; } if (l <= 0.5f) { s = (max_value - min_value) / (min_value + max_value); } else { s = (max_value - min_value) / (2.0f - (min_value + max_value)); } if (max_value == red) { h = (60.0f * (green - blue)) / (max_value - min_value); } else if (max_value == green) { h = 120.0f + (60.0f * (blue - red)) / (max_value - min_value); } else { h = 240.0f + (60.0f * (red - green)) / (max_value - min_value); } }
ColorHSVx<float, Colorf>::ColorHSVx(const Colorf &color) { float red = color.get_red(); float green = color.get_green(); float blue = color.get_blue(); const float min_value = min(red,min(blue, green)); const float max_value = max(red,max(blue, green)); a = color.get_alpha(); if (min_value == max_value) { h = 0.0f; s = 0.0f; v = min_value; return; } s = max_value != 0.0f ? (max_value - min_value) / max_value : 0.0f; v = max_value; float r = float(max_value - red) / (max_value - min_value); float g = float(max_value - green) / (max_value - min_value); float b = float(max_value - blue) / (max_value - min_value); if (max_value == red) { h = (60.0f * (b - g)); } else if (max_value == green) { h = (60.0f * (2.0f + r - b)); } else { h = (60.0f * (4.0f + g - r)); } }
void CollisionOutline::draw( float x, float y, const Colorf &color, Canvas &canvas) { GraphicContext &gc = canvas.get_gc(); // Draw collision outline (Contours are assumed as closed polygons, hence we use line-loop) for(unsigned int i = 0; i < impl->contours.size(); i++) { // Draw the contour unsigned int numpoints = impl->contours[i].get_points().size(); for(unsigned int s = 0; s < numpoints; s++) { const Pointf &p1 = impl->contours[i].get_points()[s]; const Pointf &p2 = impl->contours[i].get_points()[(s+1) % numpoints]; canvas.draw_line(x + p1.x + 0.5f, y + p1.y + 0.5f, x + p2.x + 0.5f, y + p2.y + 0.5f, color); } } Colorf colorinv(1.0f-color.get_red(),1.0f-color.get_green(),1.0f-color.get_blue()); for(unsigned int i = 0; i < impl->contours.size(); i++) { unsigned int numpoints = impl->contours[i].get_points().size(); // Add points (as opposite color) for(unsigned int s1 = 0; s1 < numpoints; s1++) { const Pointf &p1 = impl->contours[i].get_points()[s1]; canvas.draw_point(x + p1.x + 0.5f, y + p1.y + 0.5f, colorinv); } } }