contract PixelPainting is PixelOwnership {

    event Paint(uint256 tokenId, uint32 color);

    // Sets the color of an individual pixel
    function setPixelColor(uint256 _tokenId, uint32 _color) external {
        // check that the token id is in the range
        require(_tokenId < HEIGHT * WIDTH);
        // check that the sender is owner of the pixel
        require(_owns(msg.sender, _tokenId));
        colors[_tokenId] = _color;
    }

    // Sets the color of the pixels in an area, left to right and then top to bottom
    function setPixelAreaColor(uint256 x, uint256 y, uint256 x2, uint256 y2, uint32[] _colors) external {
        require(x2 > x && y2 > y);
        require(x2 <= WIDTH && y2 <= HEIGHT);
        require(_colors.length == (y2 - y) * (x2 - x));
        uint256 r = 0;
        for (uint256 i = y; i < y2; i++) {
            uint256 tokenId = i * WIDTH;
            for (uint256 j = x; j < x2; j++) {
                if (_owns(msg.sender, tokenId + j)) {
                    uint32 color = _colors[r];
                    colors[tokenId + j] = color;
                    Paint(tokenId + j, color);
                }
                r++;
            }
        }
    }

    // Returns the color of a given pixel
    function getPixelColor(uint256 _tokenId) external view returns (uint32 color) {
        require(_tokenId < HEIGHT * WIDTH);
        color = colors[_tokenId];
    }

    // Returns the colors of the pixels in an area, left to right and then top to bottom
    function getPixelAreaColor(uint256 x, uint256 y, uint256 x2, uint256 y2) external view returns (uint32[] result) {
        require(x2 > x && y2 > y);
        require(x2 <= WIDTH && y2 <= HEIGHT);
        result = new uint32[]((y2 - y) * (x2 - x));
        uint256 r = 0;
        for (uint256 i = y; i < y2; i++) {
            uint256 tokenId = i * WIDTH;
            for (uint256 j = x; j < x2; j++) {
                result[r] = colors[tokenId + j];
                r++;
            }
        }
    }
}
function getPixelColor(uint256 _tokenId) external view returns (uint32 color) {
    require(_tokenId < HEIGHT * WIDTH);
    color = colors[_tokenId];
}

// Returns the color of a given pixel