Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Color #194

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open

Color #194

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/adapters/controllers/page_controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ QImage PageController::renderPage()
return m_pageImage;

auto zoom = m_matrix.a;
m_pageImage = utils::qImageFromPixmap(m_pageGenerator.renderPage(zoom));
m_pageImage = utils::qImageFromPixmap(m_pageGenerator.renderPage(zoom, "#CECECE")); // Require custom color passed from qml

auto xOffset = m_pageGenerator.getPageXOffset();
auto yOffset = m_pageGenerator.getPageYOffset();
Expand Down
2 changes: 1 addition & 1 deletion src/application/core/metadata_extractor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ QImage MetadataExtractor::getCover()
try
{
core::PageGenerator page(m_document.get(), 0);
return utils::qImageFromPixmap(page.renderPage(1.0));
return utils::qImageFromPixmap(page.renderPage(1.0, "#FFFFFF")); // #FFFFFF is white color for default cover
}
catch(...)
{
Expand Down
24 changes: 23 additions & 1 deletion src/application/core/page_generator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,18 @@ void PageGenerator::setupLinks()
}
}

mupdf::FzPixmap PageGenerator::renderPage(float zoom)
mupdf::FzPixmap PageGenerator::renderPage(float zoom, const std::string& hexColor)
{
// Create matrix with zoom
mupdf::FzMatrix matrix;
matrix.a = zoom;
matrix.d = zoom;

auto pixmap = getEmptyPixmap(matrix);

// Set initial color of pixmap to custom rgb hex code
pixmap.fz_fill_pixmap_with_color(mupdf::FzColorspace::Fixed_RGB, convertHexToRGB(hexColor).data(), mupdf::FzColorParams());

auto drawDevice = mupdf::fz_new_draw_device(mupdf::FzMatrix(), pixmap);

// Determine the page offset the first time we render the page
Expand Down Expand Up @@ -115,12 +119,30 @@ mupdf::FzPixmap PageGenerator::renderPage(float zoom)
m_displayList.fz_run_display_list(drawDevice, matrix, rect, cookie);
drawDevice.fz_close_device();

// A bad attempt to change text color....
// pixmap.fz_tint_pixmap(0xF4F4F4, 0x000000);

if(m_invertColor)
pixmap.fz_invert_pixmap();

return pixmap;
}

// Convert hex color to acceptable rgb format for colorspace
std::array<float, 3> PageGenerator::convertHexToRGB(const std::string& hex)
{
std::array<float, 3> rgb = {0.0f, 0.0f, 0.0f};
if (hex[0] == '#') {
std::string hexColor = hex.substr(1);

// Convert hex to rgb
rgb[0] = std::stoi(hexColor.substr(0, 2), nullptr, 16) / 255.0f;
rgb[1] = std::stoi(hexColor.substr(2, 2), nullptr, 16) / 255.0f;
rgb[2] = std::stoi(hexColor.substr(4, 2), nullptr, 16) / 255.0f;
}
return rgb;
}

mupdf::FzPixmap PageGenerator::getEmptyPixmap(
const mupdf::FzMatrix& matrix) const
{
Expand Down
5 changes: 4 additions & 1 deletion src/application/core/page_generator.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once
#include <QList>
#include <QPair>
#include <array>
#include <string>
#include <vector>
#include "application_export.hpp"
Expand Down Expand Up @@ -29,7 +30,7 @@ class APPLICATION_EXPORT PageGenerator
int getPageXOffset() const;
int getPageYOffset() const;

mupdf::FzPixmap renderPage(float zoom);
mupdf::FzPixmap renderPage(float zoom, const std::string& hexColor);
void setInvertColor(bool newInvertColor);

bool pointIsAboveText(mupdf::FzPoint point);
Expand All @@ -43,6 +44,8 @@ class APPLICATION_EXPORT PageGenerator
utils::FzPointPair getPositionsForLineSelection(mupdf::FzPoint point);
std::string getTextFromSelection(mupdf::FzPoint start, mupdf::FzPoint end);

std::array<float, 3> convertHexToRGB(const std::string& hex);

private:
void setupDisplayList(const mupdf::FzRect& boundPage);
void setupTextPage(int pageNumber);
Expand Down