make colors vector based instead of QColor based
This commit is contained in:
@@ -128,6 +128,7 @@ namespace noggit
|
||||
ret->REGISTER_TYPE_CONVERTER(Decimal, Color);
|
||||
ret->REGISTER_TYPE_CONVERTER(Color, String);
|
||||
ret->REGISTER_TYPE_CONVERTER(Color, Vector4D);
|
||||
ret->REGISTER_TYPE_CONVERTER(Vector4D, Color);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
@@ -12,7 +12,7 @@ ColorMathNode::ColorMathNode()
|
||||
: BaseNode()
|
||||
{
|
||||
setName("ColorMathNode");
|
||||
setCaption("Color Mix");
|
||||
setCaption("Color Math");
|
||||
_validation_state = NodeValidationState::Valid;
|
||||
|
||||
_operation = new QComboBox(&_embedded_widget);
|
||||
@@ -36,62 +36,64 @@ ColorMathNode::ColorMathNode()
|
||||
|
||||
void ColorMathNode::compute()
|
||||
{
|
||||
QColor color_1 = _in_ports[2].in_value.lock() ? static_cast<ColorData*>(_in_ports[2].in_value.lock().get())->value()
|
||||
: static_cast<color_widgets::ColorSelector*>(_in_ports[2].default_widget)->color();
|
||||
QColor q_color_1 = static_cast<color_widgets::ColorSelector*>(_in_ports[2].default_widget)->color();
|
||||
glm::vec4 color_1 = _in_ports[2].in_value.lock() ? static_cast<ColorData*>(_in_ports[2].in_value.lock().get())->value()
|
||||
: glm::vec4(q_color_1.redF(), q_color_1.greenF(), q_color_1.blueF(), q_color_1.alphaF());
|
||||
|
||||
QColor color_2 = _in_ports[3].in_value.lock() ? static_cast<ColorData*>(_in_ports[3].in_value.lock().get())->value()
|
||||
: static_cast<color_widgets::ColorSelector*>(_in_ports[3].default_widget)->color();
|
||||
QColor q_color_2 = static_cast<color_widgets::ColorSelector*>(_in_ports[3].default_widget)->color();
|
||||
glm::vec4 color_2 = _in_ports[3].in_value.lock() ? static_cast<ColorData*>(_in_ports[3].in_value.lock().get())->value()
|
||||
: glm::vec4(q_color_2.redF(), q_color_2.greenF(), q_color_2.blueF(), q_color_2.alphaF());
|
||||
|
||||
bool clamp = _in_ports[1].in_value.lock() ? static_cast<BooleanData*>(_in_ports[1].in_value.lock().get())->value()
|
||||
: static_cast<QCheckBox*>(_in_ports[3].default_widget)->isChecked();
|
||||
|
||||
double factor = _in_ports[0].in_value.lock() ? static_cast<DecimalData*>(_in_ports[0].in_value.lock().get())->value()
|
||||
: static_cast<QDoubleSpinBox*>(_in_ports[0].default_widget)->value();
|
||||
QColor result;
|
||||
glm::vec4 result;
|
||||
|
||||
switch (_operation->currentIndex())
|
||||
{
|
||||
case 0: // Mix
|
||||
result.setRed(color_1.red() * (1.0 - factor) + color_2.red() * factor);
|
||||
result.setGreen(color_1.green() * (1.0 - factor) + color_2.green() * factor);
|
||||
result.setBlue(color_1.blue() * (1.0 - factor) + color_2.blue() * factor);
|
||||
result.setAlpha(color_1.alpha() * (1.0 - factor) + color_2.alpha() * factor);
|
||||
for (int i = 0; i < 4; ++i)
|
||||
{
|
||||
result[i] = color_1[i] * (1.0 - factor) + color_2[i] * factor;
|
||||
}
|
||||
break;
|
||||
|
||||
case 1: // Add
|
||||
result.setRed(color_1.red() + color_2.red() * factor);
|
||||
result.setGreen(color_1.green() + color_2.green() * factor);
|
||||
result.setBlue(color_1.blue() + color_2.blue() * factor);
|
||||
result.setAlpha(color_1.alpha() + color_2.alpha() * factor);
|
||||
for (int i = 0; i < 4; ++i)
|
||||
{
|
||||
result[i] = color_1[i] + color_2[i] * factor;
|
||||
}
|
||||
break;
|
||||
|
||||
case 2: // Subtract
|
||||
result.setRed(color_1.red() - color_2.red() * factor);
|
||||
result.setGreen(color_1.green() - color_2.green() * factor);
|
||||
result.setBlue(color_1.blue() - color_2.blue() * factor);
|
||||
result.setAlpha(color_1.alpha() - color_2.alpha() * factor);
|
||||
for (int i = 0; i < 4; ++i)
|
||||
{
|
||||
result[i] = color_1[i] - color_2[i] * factor;
|
||||
}
|
||||
break;
|
||||
|
||||
case 3: // Multiply
|
||||
result.setRed(color_1.red() * (color_2.red() * factor));
|
||||
result.setGreen(color_1.green() * (color_2.green() * factor));
|
||||
result.setBlue(color_1.blue() * (color_2.blue() * factor));
|
||||
result.setAlpha(color_1.alpha() * (color_2.alpha() * factor));
|
||||
for (int i = 0; i < 4; ++i)
|
||||
{
|
||||
result[i] = color_1[i] * (color_2[i] * factor);
|
||||
}
|
||||
break;
|
||||
case 4: // Divide
|
||||
result.setRed(color_1.red() / (color_2.red() * factor));
|
||||
result.setGreen(color_1.green() / (color_2.green() * factor));
|
||||
result.setBlue(color_1.blue() / (color_2.blue() * factor));
|
||||
result.setAlpha(color_1.alpha() / (color_2.alpha() * factor));
|
||||
for (int i = 0; i < 4; ++i)
|
||||
{
|
||||
result[i] = color_1[i] / (color_2[i] * factor);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
if (clamp)
|
||||
{
|
||||
result.setRed(std::max(std::min(result.red(), 255), 0));
|
||||
result.setGreen(std::max(std::min(result.green(), 255), 0));
|
||||
result.setBlue(std::max(std::min(result.blue(), 255), 0));
|
||||
result.setAlpha(std::max(std::min(result.alpha(), 255), 0));
|
||||
for (int i = 0; i < 4; ++i)
|
||||
{
|
||||
result[i] = std::max(std::min(result[i], 1.0f), 0.0f);
|
||||
}
|
||||
}
|
||||
|
||||
_out_ports[0].out_value = std::make_shared<ColorData>(result);
|
||||
|
||||
@@ -327,15 +327,16 @@ struct DefaultColorWidget
|
||||
return new color_widgets::ColorSelector(parent);
|
||||
}
|
||||
|
||||
static QColor value(QWidget* widget)
|
||||
static glm::vec4 value(QWidget* widget)
|
||||
{
|
||||
return static_cast<color_widgets::ColorSelector*>(widget)->color();
|
||||
QColor color = static_cast<color_widgets::ColorSelector*>(widget)->color();
|
||||
return glm::vec4(color.redF(), color.greenF(), color.blueF(), color.alphaF());
|
||||
}
|
||||
|
||||
static void setValue(QWidget* widget, QJsonValue& value)
|
||||
{
|
||||
auto array = value.toArray();
|
||||
QColor color = QColor::fromRgb(array[0].toInt(), array[1].toInt(), array[2].toInt(), array[3].toInt());
|
||||
QColor color = QColor::fromRgbF(array[0].toDouble(), array[1].toDouble(), array[2].toDouble(), array[3].toDouble());
|
||||
static_cast<color_widgets::ColorSelector*>(widget)->setColor(color);
|
||||
}
|
||||
|
||||
@@ -344,10 +345,10 @@ struct DefaultColorWidget
|
||||
QJsonArray array = QJsonArray();
|
||||
QColor color = static_cast<color_widgets::ColorSelector*>(widget)->color();
|
||||
|
||||
array.push_back(color.red());
|
||||
array.push_back(color.green());
|
||||
array.push_back(color.blue());
|
||||
array.push_back(color.alpha());
|
||||
array.push_back(color.redF());
|
||||
array.push_back(color.greenF());
|
||||
array.push_back(color.blueF());
|
||||
array.push_back(color.alphaF());
|
||||
|
||||
json_obj[name.c_str()] = array;
|
||||
}
|
||||
@@ -355,7 +356,7 @@ struct DefaultColorWidget
|
||||
static void fromJson(QWidget* widget, const QJsonObject& json_obj, const std::string& name)
|
||||
{
|
||||
QJsonArray array = json_obj[name.c_str()].toArray();
|
||||
QColor color = QColor::fromRgb(array[0].toInt(), array[1].toInt(), array[2].toInt(), array[3].toInt());
|
||||
QColor color = QColor::fromRgbF(array[0].toDouble(), array[1].toDouble(), array[2].toDouble(), array[3].toDouble());
|
||||
static_cast<color_widgets::ColorSelector*>(widget)->setColor(color);
|
||||
}
|
||||
};
|
||||
@@ -437,7 +438,7 @@ DECLARE_NODE_DATA_TYPE(procedure, Procedure, std::string, DefaultProcedureWidget
|
||||
DECLARE_NODE_DATA_TYPE(list, List, std::vector<std::shared_ptr<NodeData>>*, NoDefaultWidget);
|
||||
|
||||
// Custom types
|
||||
DECLARE_NODE_DATA_TYPE(color, Color, QColor, DefaultColorWidget);
|
||||
DECLARE_NODE_DATA_TYPE(color, Color, glm::vec4, DefaultColorWidget);
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -67,23 +67,23 @@ struct BasicDataConverter
|
||||
template<typename T_from>
|
||||
struct ColorIntegerConverter
|
||||
{
|
||||
static QColor convert(T_from const& value) { return QColor::fromRgb(value, value, value, value); }
|
||||
static glm::vec4 convert(T_from const& value) { return glm::vec4(value / 255.0, value / 255.0, value / 255.0, value / 255.0); }
|
||||
};
|
||||
|
||||
|
||||
struct ColorDecimalConverter
|
||||
{
|
||||
static QColor convert(double const& value) { return QColor::fromRgbF(value, value, value, value); }
|
||||
static glm::vec4 convert(double const& value) { return glm::vec4(value, value, value, value); }
|
||||
};
|
||||
|
||||
struct ColorStringConverter
|
||||
{
|
||||
static std::string convert(QColor const& value) { return (boost::format("Color<%d, %d, %d, %d>") % value.red() % value.green() % value.blue() % value.alpha()).str(); }
|
||||
static std::string convert(glm::vec4 const& value) { return (boost::format("Color<%d, %d, %d, %d>") % value[0] % value[1] % value[2] % value[3]).str(); }
|
||||
};
|
||||
|
||||
struct ColorVector4DConverter
|
||||
{
|
||||
static glm::vec4 convert(QColor const& value) { return glm::vec4(value.redF(), value.greenF(), value.blueF(), value.alphaF()); };
|
||||
static glm::vec4 convert(glm::vec4 const& value) { return value; };
|
||||
};
|
||||
|
||||
|
||||
@@ -131,6 +131,7 @@ DECLARE_TYPE_CONVERTER_EXT(UnsignedInteger, Color, ColorIntegerConverter<unsigne
|
||||
DECLARE_TYPE_CONVERTER_EXT(Decimal, Color, ColorIntegerConverter<double>)
|
||||
DECLARE_TYPE_CONVERTER_EXT(Color, String, ColorStringConverter)
|
||||
DECLARE_TYPE_CONVERTER_EXT(Color, Vector4D, ColorVector4DConverter)
|
||||
DECLARE_TYPE_CONVERTER_EXT(Vector4D, Color, ColorVector4DConverter)
|
||||
|
||||
|
||||
#endif //NOGGIT_GENERICTYPECONVERTER_HPP
|
||||
|
||||
Reference in New Issue
Block a user