<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def __write_palette(self, outfile):
 """ Write``PLTE`` and if necessary a ``tRNS`` chunk to. This method should be called only from ``write_idat`` method or chunk order will be ruined. """

        # We must have true bitdepth within palette to use sBIT with palette
        # if self.pixbitdepth != 8:
        #     write_chunk(outfile, 'sBIT',
        #             bytearray_to_bytes(bytearray((self.pixbitdepth,) * 3)))
        p = bytearray()
        t = bytearray()

        for x in self.palette:
            p.extend(x[0:3])
            if len(x) > 3:
                t.append(x[3])

        write_chunk(outfile, 'PLTE', bytearray_to_bytes(p))
        if t:
            # tRNS chunk is optional. Only needed if palette entries
            # have alpha.
            write_chunk(outfile, 'tRNS', bytearray_to_bytes(t))