Skip to main content
Skip table of contents

Color creation and conversion

📌 Overview

This Mako example shows how to create colors in different ways, including creating a spot color. It produces a PDF with colored blocks of the colors made.

⌨️ Sample Code

This sample produces a PDF with colored blocks of the colors made using the different methods. Also available at ColorConversion.cpp.

Click to see this C++ sample...
CPP
/* -----------------------------------------------------------------------
 *  <copyright file="ColourConversion.cpp" company="Global Graphics Software Ltd">
 *      Copyright (c) 2023 Global Graphics Software Ltd. All rights reserved.
 *  </copyright>
 *  <summary>
 *  This example is provided on an "as is" basis and without warranty of any kind.
 *  Global Graphics Software Ltd. does not warrant or make any representations
 *  regarding the use or results of use of this example.
 *  </summary>
 * -----------------------------------------------------------------------
 */

#include <iostream>
#include <jawsmako/jawsmako.h>
#include <jawsmako/pdfoutput.h>
#include <edl/icolormanager.h>

using namespace JawsMako;
using namespace EDL;

int wmain(int argc, wchar_t* argv[])
{
    try
    {
        // Instantiate Mako
        const auto jawsMako = IJawsMako::create();
        IJawsMako::enableAllFeatures(jawsMako);
        const auto cmm = IColorManager::get(jawsMako);

        const auto deviceCmykSpace = IDOMColorSpaceDeviceCMYK::create(jawsMako);

        // Create an LAB colour space with D65 white point and -128 to 127 ranges for a and b values
        const auto labColorSpace = IDOMColorSpaceLAB::create(jawsMako, 0.9504f, 1.0f, 1.0888f, 0.0, 0.0, 0.0, -128, 127, -128, 127);

        // Create a device CMYK colorspace from an ICC profile
        const auto iccBasedColorSpace = IDOMColorSpaceICCBased::create(
            jawsMako, IDOMICCProfile::create(
                jawsMako, IInputStream::createFromFile(
                    jawsMako, R"(C:\Windows\System32\spool\drivers\color\WebCoatedFOGRA28.icc)")));

        // Create an LAB colour
        const auto pantoneBlue072C_lab = IDOMColor::create(jawsMako, labColorSpace, 1.0, 17.64, 43.0, -76.0);

        // Make a copy and convert to ICC space
        auto pantoneBlue072C_fogra = clone(pantoneBlue072C_lab, jawsMako);
        pantoneBlue072C_fogra->setColorSpace(iccBasedColorSpace,eRelativeColorimetric, eBPCDefault, jawsMako);

        // Create colorants for a DeviceN color (spot color)
        const IDOMColorSpaceDeviceN::CColorantInfo pantoneBlue072C("PANTONE BLUE 072 C", 3, 17.64, 43.0, -76.0);
        IDOMColorSpaceDeviceN::CColorantInfoVect colorants;
        colorants.append(pantoneBlue072C);

        // Create a deviceN colour space
        const auto deviceNSpace = IDOMColorSpaceDeviceN::create(jawsMako, colorants, labColorSpace);

        // Create a spot
        const auto pantoneBlue072C_spot = IDOMColor::create(jawsMako, deviceNSpace, 1.0, 1.0);

        // Create a PDF with a swatch of that colour
        const auto documentAssembly = IDocumentAssembly::create(jawsMako);
        const auto document = IDocument::create(jawsMako);
        documentAssembly->appendDocument(document);
        const auto page = IPage::create(jawsMako);
        document->appendPage(page);
        const auto fixedPage = IDOMFixedPage::create(jawsMako);
        page->setContent(fixedPage);

        // Add content. Three blocks, using the colours we have created
        IDOMBrushPtr solidBrush = IDOMSolidColorBrush::create(jawsMako, pantoneBlue072C_lab);
        FRect swatchPositionAndSize = FRect(48.0, 192.0, 192.0, 192.0);
        auto  swatch = IDOMPathNode::createFilled(jawsMako, IDOMPathGeometry::create(jawsMako, swatchPositionAndSize), solidBrush);
        fixedPage->appendChild(swatch);

        solidBrush = IDOMSolidColorBrush::create(jawsMako, pantoneBlue072C_spot);
        swatchPositionAndSize = FRect(288.0, 192.0, 192.0, 192.0);
        swatch = IDOMPathNode::createFilled(jawsMako, IDOMPathGeometry::create(jawsMako, swatchPositionAndSize), solidBrush);
        fixedPage->appendChild(swatch);

        solidBrush = IDOMSolidColorBrush::create(jawsMako, pantoneBlue072C_fogra);
        swatchPositionAndSize = FRect(528.0, 192.0, 192.0, 192.0);
        swatch = IDOMPathNode::createFilled(jawsMako, IDOMPathGeometry::create(jawsMako, swatchPositionAndSize), solidBrush);
        fixedPage->appendChild(swatch);

        // Write PDF
        IPDFOutput::create(jawsMako)->writeAssembly(documentAssembly, "test.pdf");
    }
    catch (IError& e)
    {
        const String errorFormatString = getEDLErrorString(e.getErrorCode());
        std::wcerr << L"Exception thrown: " << e.getErrorDescription(errorFormatString) << std::endl;
        return static_cast<int>(e.getErrorCode());
    }
    catch (std::exception& e)
    {
        std::wcerr << L"std::exception thrown: " << e.what() << std::endl;
        return 1;
    }

    return 0;
}

☑️ Conclusion

The Mako SDK provides versatile methods for color creation and conversion, allowing users to work with various color spaces and profiles. By following the provided sample code, users can create colors in different ways, including spot colors, and convert them across color spaces. This capability is essential for producing accurate and consistent color outputs in PDFs.

📚 Additional Resources

If you need additional help, see our API documentation for detailed information on class/method usage, or raise a support ticket via our customer portal.

JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.