ViewSolutions
Loading...
Searching...
No Matches
colorpicker.cpp
Go to the documentation of this file.
1//#
2//# Copyright (C) 2020-2025 QuasarApp.
3//# Distributed under the GPLv3 software license, see the accompanying
4//# Everyone is permitted to copy and distribute verbatim copies
5//# of this license document, but changing it is not allowed.
6//#
7
8#include "colorpicker.h"
9
10#include <QColor>
11#include <QImage>
12#include <QUrl>
13namespace ViewSolutions {
14
18
19QColor ColorPicker::pick(int x, int y, const QImage &img) const {
20 return img.pixelColor(x, y);
21}
22
23QColor ColorPicker::pick(const QImage &img, int density) const {
24 int stepX = img.width() / density;
25 int stepY = img.height() / density;
26
27 if (!(stepX && stepY)) {
28 return QColor::fromRgb(0);
29 }
30
31 int A = 0,
32 R = 0,
33 G = 0,
34 B = 0;
35
36 for (int x = stepX - 1; x < img.width(); x += stepX) {
37 for (int y = stepY - 1; y < img.height(); y += stepY) {
38 int pixel = img.pixel(x, y);
39 A += (pixel & 0xFF000000) >> 24;
40 R += (pixel & 0x00FF0000) >> 16;
41 G += (pixel & 0x0000FF00) >> 8;
42 B += pixel & 0x000000FF;
43 }
44 }
45
46 int count = density * density;
47 return QColor::fromRgba(((A / count) << 24) |
48 ((R / count) << 16) |
49 ((G / count) << 8) |
50 B / count);
51}
52
53QColor ColorPicker::pick(const QString &img) const {
54 if (img.left(3).compare("qrc") == 0) {
55 return pick(QImage(img.right(img.size() - 3)));
56 }
57 return pick(QImage(img));
58}
59
60}
QColor pick(int x, int y, const QImage &img) const
pick Get color from point.
the ViewSolutions namespace