1 /***************************************************************************
2 * Copyright (C) 2010 by Simon Andreas Eugster (simon.eu@gmail.com) *
3 * This file is part of kdenlive. See www.kdenlive.org. *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 ***************************************************************************/
12 #include "colortools.h"
14 ColorTools::ColorTools()
20 QImage ColorTools::yuvColorWheel(const QSize &size, unsigned char Y, float scaling, bool modifiedVersion, bool circleOnly)
22 QImage wheel(size, QImage::Format_ARGB32);
23 if (size.width() == 0 || size.height() == 0) {
24 qCritical("ERROR: Size of the color wheel must not be 0!");
27 wheel.fill(qRgba(0,0,0,0));
29 double dr, dg, db, du, dv, dmax;
31 const int w = size.width();
32 const int h = size.height();
33 const float w2 = (float)w/2;
34 const float h2 = (float)h/2;
36 for (int u = 0; u < w; u++) {
37 // Transform u from {0,...,w} to [-1,1]
38 du = (double) 2*u/(w-1) - 1;
41 for (int v = 0; v < h; v++) {
42 dv = (double) 2*v/(h-1) - 1;
46 // Ellipsis equation: x²/a² + y²/b² = 1
47 // Here: x=ru, y=rv, a=w/2, b=h/2, 1=rr
48 // For rr > 1, the point lies outside. Don't draw it.
51 rr = ru*ru/(w2*w2) + rv*rv/(h2*h2);
57 // Calculate the RGB values from YUV
59 dg = Y - 100.6*du - 148*dv;
62 if (modifiedVersion) {
63 // Scale the RGB values down, or up, to max 255
65 if (fabs(dg) > dmax) dmax = fabs(dg);
66 if (fabs(db) > dmax) dmax = fabs(db);
74 // Avoid overflows (which would generate intersting patterns).
75 // Note that not all possible (y,u,v) values with u,v \in [-1,1]
76 // have a correct RGB representation, therefore some RGB values
77 // may exceed {0,...,255}.
81 if (dr > 255) dr = 255;
82 if (dg > 255) dg = 255;
83 if (db > 255) db = 255;
85 wheel.setPixel(u, (h-v-1), qRgba(dr, dg, db, 255));
89 emit signalWheelCalculationFinished();