flossColor addSquareColorVersion_0_9_2_16::transform(const flossColor& color) const { if (color.type() == flossDMC || !::colorIsDmc(color.color())) { return color; } else { // it's a dmc color not marked as DMC return flossColor(color.color(), flossDMC); } }
void changeOneTool::activeImageClick(QMouseEvent* event) { const int x = event->x(); const int y = event->y(); const int originalImageWidth = parent()->curImage_->originalWidth(); const int originalImageHeight = parent()->curImage_->originalHeight(); squareImageLabel* label = parent()->activeSquareLabel(); const int labelWidth = label->width(); const int labelHeight = label->height(); int originalX = (x * originalImageWidth)/labelWidth; int originalY = (y * originalImageHeight)/labelHeight; const int originalDimension = parent()->curImage_->originalDimension(); const int boxX = originalX/originalDimension; const int boxY = originalY/originalDimension; const QRgb oldColor = ::colorFromScaledImageCoords(x, y, labelWidth, labelHeight, parent()->curImage_->image()); const Qt::MouseButton mouseButton = event->button(); if (mouseButton == Qt::LeftButton) { dragCache_.cacheIsActive = true; dragCache_.labelWidth = labelWidth; dragCache_.labelHeight = labelHeight; dragCache_.imageWidth = originalImageWidth; dragCache_.imageHeight = originalImageHeight; dragCache_.squareDim = originalDimension; // this won't be used until we actually drag QMatrix matrix; matrix.scale(static_cast<qreal>(labelWidth)/originalImageWidth, static_cast<qreal>(labelHeight)/originalImageHeight); matrix = QImage::trueMatrix(matrix, originalImageWidth, originalImageHeight); dragCache_.matrix = matrix; const pairOfInts boxCoordinates(boxX, boxY); const flossColor newColor = parent()->toolDock_->getToolLabelColor(); dragCache_.newColor = newColor; dragCache_.squaresVisited.insert(boxCoordinates); label->setSquaresColor(newColor.qrgb()); label->addSquare(boxCoordinates); const int d = parent()->roughCurDim(); label->update(x-d, y-d, 2*d, 2*d); } else if (mouseButton == Qt::MidButton) { // make originals upper left corner of the square clicked originalX = boxX * originalDimension; originalY = boxY * originalDimension; parent()->requestSquareColor(originalX, originalY, originalDimension, oldColor); } else if (mouseButton == Qt::RightButton) { updateToolColor(oldColor); } }
dockListUpdate mutableSquareImageContainer::fillRegion(int x, int y, flossColor newColor) { const triC oldColor = image_.pixel(x, y); if (newColor == oldColor) { return dockListUpdate(); } const QVector<pairOfInts> coordinates = ::fillRegion(&image_, x, y, newColor.qrgb(), originalDimension_); const bool colorAdded = addColor(newColor); const flossColor oldFlossColor = getFlossColorFromColor(oldColor); addToHistory(historyItemPtr(new fillRegionHistoryItem(oldFlossColor, newColor, colorAdded, coordinates))); colorListCheckNeeded_ = true; return dockListUpdate(newColor.color(), colorAdded); }
typedFloss rgbToFloss(const flossColor& color) { // Keep this in sync with rgbToFloss(const QVector<flossColor>& colors). const flossTypeValue type = color.type().value(); if (type == flossDMC) { const QVector<floss> dmcColors = ::initializeDMC(); const int index = dmcColors.indexOf(floss(color.color())); if (index != -1) { const floss thisDmcFloss = dmcColors[index]; return typedFloss(thisDmcFloss, flossDMC); } } else if (type == flossAnchor) { const QVector<floss> anchorColors = ::initializeAnchor(); const int index = anchorColors.indexOf(floss(color.color())); if (index != -1) { const floss thisAnchorFloss = anchorColors[index]; return typedFloss(thisAnchorFloss, flossAnchor); } } return typedFloss(floss(color.color()), flossVariable); }
QVector<typedFloss> rgbToFloss(const QVector<flossColor>& colors) { // Keep this in sync with rgbToFloss(const flossColor& color) - we're keeping // this version separate so that we don't need to load the DMC/Anchor color // list for each color on this list. // Also, WARNING: there was a bug in an old version of cstitch which in // certain cases allowed a non-floss color to be labeled as floss, so now // forevermore we need to handle that case just for any projects saved with // the bug. :( QVector<typedFloss> returnFloss; returnFloss.reserve(colors.size()); QVector<floss> dmcColors; QVector<floss> anchorColors; for (int i = 0, size = colors.size(); i < size; ++i) { const flossColor color = colors[i]; switch (color.type().value()) { case flossDMC: { if (dmcColors.isEmpty()) { dmcColors = ::initializeDMC(); } const int index = dmcColors.indexOf(floss(color.color())); if (index != -1) { const floss thisDmcFloss = dmcColors[index]; returnFloss.push_back(typedFloss(thisDmcFloss, flossDMC)); } else { returnFloss.push_back(typedFloss(floss(color.color()), flossVariable)); } break; } case flossAnchor: { if (anchorColors.isEmpty()) { anchorColors = ::initializeAnchor(); } const int index = anchorColors.indexOf(floss(color.color())); if (index != -1) { const floss thisAnchorFloss = anchorColors[index]; returnFloss.push_back(typedFloss(thisAnchorFloss, flossAnchor)); } else { returnFloss.push_back(typedFloss(floss(color.color()), flossVariable)); } break; } case flossVariable: returnFloss.push_back(typedFloss(floss(color.color()), flossVariable)); break; } } return returnFloss; }
dockListUpdate mutableSquareImageContainer:: commitChangeOneDrag(const QSet<pairOfInts>& squares, flossColor newColor) { const QRgb newRgbColor = newColor.qrgb(); const bool colorAdded = addColor(newColor); QVector<pixel> historyPixels; QVector<triC> pixelColors; for (QSet<pairOfInts>::const_iterator it = squares.begin(), end = squares.end(); it != end; ++it) { const int x = it->x() * originalDimension_; const int y = it->y() * originalDimension_; const QRgb thisColor = image_.pixel(x, y); pixelColors.push_back(thisColor); historyPixels.push_back(pixel(thisColor, pairOfInts(x, y))); } ::changeBlocks(&image_, historyPixels, newRgbColor, originalDimension_); addToHistory(historyItemPtr(new changeOneHistoryItem(newColor, colorAdded, historyPixels))); colorListCheckNeeded_ = true; return dockListUpdate(newRgbColor, colorAdded); }
dockListUpdate mutableSquareImageContainer::changeColor(QRgb oldColor, flossColor newFlossColor) { const QRgb newColor = newFlossColor.color().qrgb(); if (oldColor == newColor) { return dockListUpdate(); } const QVector<pairOfInts> changedSquares = ::changeColor(&image_, oldColor, newColor, originalDimension_); if (!changedSquares.empty()) { const bool colorAdded = addColor(newFlossColor); const flossColor oldFlossColor = removeColor(oldColor); addToHistory(historyItemPtr(new changeAllHistoryItem(oldFlossColor, newFlossColor, colorAdded, changedSquares))); return dockListUpdate(newColor, colorAdded, oldColor); } else { return dockListUpdate(); } }
void squareToolDock::setToolLabelColor(const flossColor& color) { fillToolColorSwatch(color.qrgb()); }
QVector<typedFloss> rgbToFloss(const QVector<flossColor>& rgbColors) { QVector<typedFloss> returnFloss; returnFloss.reserve(rgbColors.size()); const QVector<floss> dmcFloss = ::initializeDMC(); const QVector<int> flossCodes = ::rgbToCode(rgbColors); for (int i = 0, size = rgbColors.size(); i < size; ++i) { const flossColor thisColor = rgbColors[i]; if (thisColor.type() == flossDMC) { const int dmcIndex = dmcFloss.indexOf(floss(thisColor.color())); if (dmcIndex != -1) { // (paranoia check) const floss thisDmcFloss = dmcFloss[dmcIndex]; returnFloss.push_back(typedFloss(thisDmcFloss, flossDMC)); } else { qWarning() << "DMC color not found in rgbToFloss: " << ::ctos(thisColor.color()); returnFloss.push_back(typedFloss(-1, "ERROR", thisColor.color(), thisColor.type())); } } else { const triC closestDmcColor = ::transformColor(thisColor.color(), flossDMC); const int dmcIndex = dmcFloss.indexOf(floss(closestDmcColor)); if (dmcIndex != -1) { // (paranoia check) const floss closestDmcFloss = dmcFloss[dmcIndex]; const QString dmcApproximation = "~" + QString::number(closestDmcFloss.code()) + ":" + closestDmcFloss.name(); returnFloss.push_back(typedFloss(flossCodes[i], dmcApproximation, thisColor.color(), thisColor.type())); } else { qWarning() << "DMC transform failed in rgbToFloss: " << ::ctos(thisColor.color()) << thisColor.type().value() << ::ctos(closestDmcColor); returnFloss.push_back(typedFloss(-1, "ERROR", thisColor.color(), thisColor.type())); } } } return returnFloss; }