/**************************************************************************
 **                                                                      **
 ** Copyright (C) 2011-2026 Lukas Spies                                  **
 ** Contact: https://photoqt.org                                         **
 **                                                                      **
 ** This file is part of PhotoQt.                                        **
 **                                                                      **
 ** PhotoQt is free software: you can redistribute it and/or modify      **
 ** it under the terms of the GNU General Public License as published by **
 ** the Free Software Foundation, either version 2 of the License, or    **
 ** (at your option) any later version.                                  **
 **                                                                      **
 ** PhotoQt is distributed in the hope that it will be useful,           **
 ** but WITHOUT ANY WARRANTY; without even the implied warranty of       **
 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        **
 ** GNU General Public License for more details.                         **
 **                                                                      **
 ** You should have received a copy of the GNU General Public License    **
 ** along with PhotoQt. If not, see <http://www.gnu.org/licenses/>.      **
 **                                                                      **
 **************************************************************************/

#include <pqc_providersvgcolor.h>
#include <pqc_providersvg.h>

PQCProviderSVGColor::PQCProviderSVGColor() : QQuickImageProvider(QQuickImageProvider::Image) {}

PQCProviderSVGColor::~PQCProviderSVGColor() {}

QImage PQCProviderSVGColor::requestImage(const QString &url, QSize *origSize, const QSize &requestedSize) {

    // we don't have a debug statement here as there would be A LOT of output all the time

    QImage ret;

    if(requestedSize.isNull()) {
        ret = QImage(1,1, QImage::Format_ARGB32);
        ret.fill(Qt::transparent);
        return ret;
    }

    const int idx = url.indexOf(":://::");

    // have a target color!
    if(idx > -1) {

        const QString fn = url.mid(idx+6);

        // extract target color and lighten or darken it
        QColor c(url.mid(0,idx));

        // create inverse color (half transparent to not be overpowering)
        QColor invc((255-c.red()), (255-c.green()), (255-c.blue()), 32);

        // request image
        PQCProviderSVG prov;
        QImage img = prov.requestImage(fn, origSize, requestedSize);

        const QRgb white = qRgb(255, 255, 255);
        const QRgb newColor = c.rgba();
        const QRgb invColor = invc.rgba();

        // loop through image and adjust colors
        for(int y = 0; y < img.height(); ++y) {
            QRgb* line = reinterpret_cast<QRgb*>(img.scanLine(y));
            for(int x = 0; x < img.width(); ++x) {
                const QRgb px = line[x];
                if((px & 0x00FFFFFF) == 0x00FFFFFF)
                    line[x] = newColor;
                else if(qAlpha(px) != 0)
                    line[x] = invColor;
            }
        }

        // done
        return img;

    }

    // no color provided? simply return image
    PQCProviderSVG prov;
    return prov.requestImage(url, origSize, requestedSize);

}
