File indexing completed on 2024-04-28 15:28:42

0001 /* This file is part of the KDE libraries
0002     Copyright (C) 2005, 2006 Ian Reinhart Geiser <geiseri@kde.org>
0003     Copyright (C) 2005, 2006 Matt Broadstone <mbroadst@gmail.com>
0004     Copyright (C) 2005, 2006 Richard J. Moore <rich@kde.org>
0005     Copyright (C) 2005, 2006 Erik L. Bunce <kde@bunce.us>
0006 
0007     This library is free software; you can redistribute it and/or
0008     modify it under the terms of the GNU Library General Public
0009     License as published by the Free Software Foundation; either
0010     version 2 of the License, or (at your option) any later version.
0011 
0012     This library is distributed in the hope that it will be useful,
0013     but WITHOUT ANY WARRANTY; without even the implied warranty of
0014     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0015     Library General Public License for more details.
0016 
0017     You should have received a copy of the GNU Library General Public License
0018     along with this library; see the file COPYING.LIB.  If not, write to
0019     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0020     Boston, MA 02110-1301, USA.
0021 */
0022 #include "color.h"
0023 
0024 #include <QDebug>
0025 #include <QColor>
0026 
0027 using namespace KJSEmbed;
0028 
0029 const KJS::ClassInfo ColorBinding::info = { "QColor", &VariantBinding::info, nullptr, nullptr };
0030 ColorBinding::ColorBinding(KJS::ExecState *exec, const QColor &value)
0031     : VariantBinding(exec, value)
0032 {
0033     StaticBinding::publish(exec, this, Color::methods());
0034     StaticBinding::publish(exec, this, VariantFactory::methods());
0035 }
0036 
0037 START_VARIANT_METHOD(callSetAlpha, QColor)
0038 value.setAlpha(KJSEmbed::extractInt(exec, args, 0));
0039 END_VARIANT_METHOD
0040 
0041 START_VARIANT_METHOD(callSetBlue, QColor)
0042 value.setBlue(KJSEmbed::extractInt(exec, args, 0));
0043 END_VARIANT_METHOD
0044 
0045 START_VARIANT_METHOD(callSetGreen, QColor)
0046 value.setGreen(KJSEmbed::extractInt(exec, args, 0));
0047 END_VARIANT_METHOD
0048 
0049 START_VARIANT_METHOD(callSetRed, QColor)
0050 value.setRed(KJSEmbed::extractInt(exec, args, 0));
0051 END_VARIANT_METHOD
0052 
0053 START_VARIANT_METHOD(callSetRgb, QColor)
0054 value.setRgb(KJSEmbed::extractInt(exec, args, 0),
0055              KJSEmbed::extractInt(exec, args, 1),
0056              KJSEmbed::extractInt(exec, args, 2),
0057              KJSEmbed::extractInt(exec, args, 3, 255));
0058 END_VARIANT_METHOD
0059 
0060 START_VARIANT_METHOD(callSetCmyk, QColor)
0061 value.setCmyk(KJSEmbed::extractInt(exec, args, 0),
0062               KJSEmbed::extractInt(exec, args, 1),
0063               KJSEmbed::extractInt(exec, args, 2),
0064               KJSEmbed::extractInt(exec, args, 3),
0065               KJSEmbed::extractInt(exec, args, 4, 255));
0066 END_VARIANT_METHOD
0067 
0068 START_VARIANT_METHOD(callSetHsv, QColor)
0069 value.setHsv(KJSEmbed::extractInt(exec, args, 0),
0070              KJSEmbed::extractInt(exec, args, 1),
0071              KJSEmbed::extractInt(exec, args, 2),
0072              KJSEmbed::extractInt(exec, args, 3, 255));
0073 END_VARIANT_METHOD
0074 
0075 START_VARIANT_METHOD(callSetNamedColor, QColor)
0076 value.setNamedColor(KJSEmbed::extractQString(exec, args, 0));
0077 END_VARIANT_METHOD
0078 
0079 START_VARIANT_METHOD(callAlpha, QColor)
0080 value.setAlpha(KJSEmbed::extractInt(exec, args, 0));
0081 END_VARIANT_METHOD
0082 
0083 START_VARIANT_METHOD(callBlue, QColor)
0084 result = KJSEmbed::createInt(exec, value.blue());
0085 END_VARIANT_METHOD
0086 
0087 START_VARIANT_METHOD(callCyan, QColor)
0088 result = KJSEmbed::createInt(exec, value.cyan());
0089 END_VARIANT_METHOD
0090 
0091 START_VARIANT_METHOD(callGreen, QColor)
0092 result = KJSEmbed::createInt(exec, value.green());
0093 END_VARIANT_METHOD
0094 
0095 START_VARIANT_METHOD(callHue, QColor)
0096 result = KJSEmbed::createInt(exec, value.hue());
0097 END_VARIANT_METHOD
0098 
0099 START_VARIANT_METHOD(callMagenta, QColor)
0100 result = KJSEmbed::createInt(exec, value.magenta());
0101 END_VARIANT_METHOD
0102 
0103 START_VARIANT_METHOD(callRed, QColor)
0104 result = KJSEmbed::createInt(exec, value.red());
0105 END_VARIANT_METHOD
0106 
0107 START_VARIANT_METHOD(callYellow, QColor)
0108 result = KJSEmbed::createInt(exec, value.yellow());
0109 END_VARIANT_METHOD
0110 
0111 START_VARIANT_METHOD(callSaturation, QColor)
0112 result = KJSEmbed::createInt(exec, value.saturation());
0113 END_VARIANT_METHOD
0114 
0115 START_VARIANT_METHOD(callDark, QColor)
0116 QColor darkColor = value.darker(KJSEmbed::extractInt(exec, args, 0, 200));
0117 result = KJSEmbed::createVariant(exec, "QColor", darkColor);
0118 END_VARIANT_METHOD
0119 
0120 START_VARIANT_METHOD(callLight, QColor)
0121 QColor darkColor = value.lighter(KJSEmbed::extractInt(exec, args, 0, 200));
0122 result = KJSEmbed::createVariant(exec, "QColor", darkColor);
0123 END_VARIANT_METHOD
0124 
0125 START_VARIANT_METHOD(callConvertTo, QColor)
0126 QColor otherColor = value.convertTo((QColor::Spec)KJSEmbed::extractInt(exec, args, 0));
0127 result = KJSEmbed::createVariant(exec, "QColor", otherColor);
0128 END_VARIANT_METHOD
0129 
0130 START_VARIANT_METHOD(callSpec, QColor)
0131 result = KJS::jsNumber(value.spec());
0132 END_VARIANT_METHOD
0133 
0134 START_METHOD_LUT(Color)
0135 {"setAlpha", 1, KJS::DontDelete | KJS::ReadOnly, &callSetAlpha},
0136 {"setBlue", 1, KJS::DontDelete | KJS::ReadOnly, &callSetBlue},
0137 {"setGreen", 1, KJS::DontDelete | KJS::ReadOnly, &callSetGreen},
0138 {"setRed", 1, KJS::DontDelete | KJS::ReadOnly, &callSetRed},
0139 {"setRgb", 4, KJS::DontDelete | KJS::ReadOnly, &callSetRgb},
0140 {"setCmyk", 5, KJS::DontDelete | KJS::ReadOnly, &callSetCmyk},
0141 {"setHsv", 4, KJS::DontDelete | KJS::ReadOnly, &callSetHsv},
0142 {"setNamedColor", 1, KJS::DontDelete | KJS::ReadOnly, &callSetNamedColor},
0143 {"alpha", 0, KJS::DontDelete | KJS::ReadOnly, &callAlpha},
0144 {"blue", 0, KJS::DontDelete | KJS::ReadOnly, &callBlue},
0145 {"cyan", 0, KJS::DontDelete | KJS::ReadOnly, &callCyan},
0146 {"green", 0, KJS::DontDelete | KJS::ReadOnly, &callGreen},
0147 {"hue", 0, KJS::DontDelete | KJS::ReadOnly, &callHue},
0148 {"magenta", 0, KJS::DontDelete | KJS::ReadOnly, &callMagenta},
0149 {"red", 0, KJS::DontDelete | KJS::ReadOnly, &callRed},
0150 {"saturation", 0, KJS::DontDelete | KJS::ReadOnly, &callSaturation},
0151 {"yellow", 0, KJS::DontDelete | KJS::ReadOnly, &callYellow},
0152 {"light", 1, KJS::DontDelete | KJS::ReadOnly, &callLight},
0153 {"dark", 1, KJS::DontDelete | KJS::ReadOnly, &callDark},
0154 {"convertTo", 1, KJS::DontDelete | KJS::ReadOnly, &callConvertTo},
0155 {"spec", 0, KJS::DontDelete | KJS::ReadOnly, &callSpec}
0156 END_METHOD_LUT
0157 
0158 START_ENUM_LUT(Color)
0159 {"Rgb", QColor::Rgb},
0160 {"Hsv", QColor::Hsv},
0161 {"Cmyk", QColor::Cmyk},
0162 {"Invalid", QColor::Invalid}
0163 END_ENUM_LUT
0164 
0165 NO_STATICS(Color)
0166 
0167 START_CTOR(Color, QColor, 0)
0168 if (args.size() == 1)
0169 {
0170     return new KJSEmbed::ColorBinding(exec, QColor(KJSEmbed::extractQString(exec, args, 0)));
0171 } else if (args.size() >= 3)
0172 {
0173     return new KJSEmbed::ColorBinding(exec,
0174                                       QColor(KJSEmbed::extractInt(exec, args, 0),
0175                                               KJSEmbed::extractInt(exec, args, 1),
0176                                               KJSEmbed::extractInt(exec, args, 2)));
0177 }
0178 
0179 if (args.size() == 4)
0180 {
0181     return new KJSEmbed::ColorBinding(exec,
0182                                       QColor(KJSEmbed::extractInt(exec, args, 0),
0183                                               KJSEmbed::extractInt(exec, args, 1),
0184                                               KJSEmbed::extractInt(exec, args, 2),
0185                                               KJSEmbed::extractInt(exec, args, 3)));
0186 }
0187 
0188 return new KJSEmbed::ColorBinding(exec, QColor());
0189 END_CTOR
0190