### Complete Palette Example with All Color Modes and Value Ranges Source: https://context7.com/nsfmc/swatch/llms.txt Provides a comprehensive example of creating a color palette that includes all supported color modes (RGB, CMYK, Gray, LAB) and demonstrates their respective value ranges. The palette is then written to an ASE file. ```python import swatch # Complete example showing all color modes and their value ranges complete_palette = [ # RGB: Three floats [0, 1] for Red, Green, Blue { 'name': 'RGB White', 'type': 'Process', 'data': {'mode': 'RGB', 'values': [1.0, 1.0, 1.0]} }, { 'name': 'RGB Red', 'type': 'Process', 'data': {'mode': 'RGB', 'values': [1.0, 0.0, 0.0]} }, # CMYK: Four floats [0, 1] for Cyan, Magenta, Yellow, Key (Black) { 'name': 'CMYK Rich Black', 'type': 'Process', 'data': {'mode': 'CMYK', 'values': [0.75, 0.68, 0.67, 0.90]} }, # Gray: One float [0, 1] where 0=black, 1=white { 'name': 'Medium Gray', 'type': 'Process', 'data': {'mode': 'Gray', 'values': [0.5]} }, # LAB: Three values - L [0, 1], A [-128, 127], B [-128, 127] { 'name': 'LAB Cyan', 'type': 'Spot', 'data': {'mode': 'LAB', 'values': [0.6, -35.0, -5.0]} } ] swatch.write(complete_palette, 'all_color_modes.ase') # Color types explained: # - 'Process': Standard mixed color (default) # - 'Global': Like Process but updates throughout document when changed # - 'Spot': Named spot color, can have tints (implicitly global) ``` -------------------------------- ### Create and Write Color Data to ASE File Source: https://context7.com/nsfmc/swatch/llms.txt Demonstrates how to create a list of color dictionaries and write them to an ASE file using the swatch library. It also shows how to write to an in-memory BytesIO buffer for immediate data retrieval. ```python import swatch import io # Create color data colors = [ { 'name': 'Spot Gold', 'type': 'Spot', 'data': { 'mode': 'LAB', 'values': [0.75, 5.0, 50.0] # L (0-1), A (-128 to 127), B (-128 to 127) } } ] # Write to file object with open('gold_swatch.ase', 'wb') as f: swatch.dump(colors, f) # Or write to BytesIO for in-memory processing buffer = io.BytesIO() swatch.dump(colors, buffer) ase_data = buffer.getvalue() print(f"Generated {len(ase_data)} bytes of ASE data") ``` -------------------------------- ### Write ASE file with Swatch Source: https://context7.com/nsfmc/swatch/llms.txt Writes a list of swatches and color groups to an Adobe Swatch Exchange (.ase) file. The input must be a list containing swatch dictionaries and/or color group dictionaries. Supports various color modes and types. ```python import swatch # Create a color palette programmatically my_palette = [ # Individual RGB color { 'name': 'Brand Red', 'type': 'Process', 'data': { 'mode': 'RGB', 'values': [0.9, 0.2, 0.1] # Values are floats between 0-1 } }, # CMYK color for print { 'name': 'Print Blue', 'type': 'Global', 'data': { 'mode': 'CMYK', 'values': [1.0, 0.5, 0.0, 0.0] # C, M, Y, K } }, # Color group with multiple swatches { 'name': 'UI Colors', 'type': 'Color Group', 'swatches': [ { 'name': 'Primary', 'type': 'Process', 'data': {'mode': 'RGB', 'values': [0.2, 0.4, 0.8]} }, { 'name': 'Secondary', 'type': 'Process', 'data': {'mode': 'RGB', 'values': [0.8, 0.3, 0.5]} }, { 'name': 'Neutral Gray', 'type': 'Process', 'data': {'mode': 'Gray', 'values': [0.5]} # Single value, 0=black, 1=white } ] } ] # Write to ASE file swatch.write(my_palette, 'my_brand_colors.ase') # Verify by reading back verified = swatch.parse('my_brand_colors.ase') print(f"Saved {len(verified)} items to ASE file") ``` -------------------------------- ### ASE to JSON and Back Round-Trip Conversion Source: https://context7.com/nsfmc/swatch/llms.txt Illustrates the round-trip conversion capability between ASE files and JSON format. This process allows for easy editing of color palettes using standard text editors or programmatic manipulation before converting back to ASE. ```python import swatch import json # Parse ASE to Python objects colors = swatch.parse('original.ase') # Export to JSON for easy editing with open('colors.json', 'w') as f: json.dump(colors, f, indent=2) # Edit the JSON file with any text editor... (Manual step) # Load modified JSON and write back to ASE with open('colors.json', 'r') as f: modified_colors = json.load(f) swatch.write(modified_colors, 'modified.ase') # Example: Modify colors programmatically colors = swatch.parse('brand.ase') # Adjust all RGB colors to be slightly darker for item in colors: if item['type'] == 'Color Group': for color in item['swatches']: if color['data']['mode'] == 'RGB': color['data']['values'] = [v * 0.9 for v in color['data']['values']] elif 'data' in item and item['data']['mode'] == 'RGB': item['data']['values'] = [v * 0.9 for v in item['data']['values']] swatch.write(colors, 'brand_darker.ase') ``` -------------------------------- ### Serialize swatch object to bytes with Swatch Source: https://context7.com/nsfmc/swatch/llms.txt Converts a swatch object to bytes suitable for writing to a file or transmitting over a network. Returns the binary ASE data without writing to disk. Useful for dynamic generation or transfer of color palettes. ```python import swatch # Create a simple color swatch colors = [ { 'name': 'Web Safe Blue', 'type': 'Process', 'data': { 'mode': 'RGB', 'values': [0.0, 0.0, 1.0] } } ] # Get binary ASE dataase_bytes = swatch.dumps(colors) # The bytes start with 'ASEF' magic header print(f"ASE data size: {len(ase_bytes)} bytes") print(f"Magic header: {ase_bytes[:4]}") # b'ASEF' # Write manually to file with open('manual_output.ase', 'wb') as f: f.write(ase_bytes) ``` -------------------------------- ### Write swatch object to file-like object with Swatch Source: https://context7.com/nsfmc/swatch/llms.txt Writes a swatch object to an open file object. This method provides more control over file handling, allowing writing to various file-like objects such as memory buffers. ```python import swatch import io # Assuming 'my_palette' is defined as in the write example # my_palette = [...] # Example using io.BytesIO to write to memory # output_buffer = io.BytesIO() # swatch.dump(my_palette, output_buffer) # ase_data = output_buffer.getvalue() # Example writing to a file # with open('output.ase', 'wb') as f: # swatch.dump(my_palette, f) ``` -------------------------------- ### Parse ASE file with Swatch Source: https://context7.com/nsfmc/swatch/llms.txt Parses an Adobe Swatch Exchange (.ase) file and returns a list of colors and color groups. Supports individual colors and color groups, preserving metadata like names and types. Handles RGB, CMYK, Grayscale, and LAB color spaces. ```python import swatch # Parse an ASE file colors = swatch.parse("solarized.ase") # Example output structure: # [ # { # 'name': 'Light Grey', # 'type': 'Process', # 'data': { # 'mode': 'Gray', # 'values': [0.75] # } # }, # { # 'name': 'Accent Colors', # 'type': 'Color Group', # 'swatches': [ # { # 'name': 'Green', # 'type': 'Process', # 'data': { # 'mode': 'CMYK', # 'values': [0.528, 0.244, 1.0, 0.043] # } # }, # { # 'name': 'Solarized Cyan', # 'type': 'Spot', # 'data': { # 'mode': 'LAB', # 'values': [0.6, -35.0, -5.0] # } # } # ] # } # ] # Iterate through parsed colors for item in colors: if item['type'] == 'Color Group': print(f"Palette: {item['name']}") for color in item['swatches']: print(f" - {color['name']}: {color['data']['mode']} {color['data']['values']}") else: print(f"Color: {item['name']}: {item['data']['mode']} {item['data']['values']}") ``` -------------------------------- ### Parse Adobe Swatch Exchange File Source: https://github.com/nsfmc/swatch/blob/master/README.rst Parses an Adobe Swatch Exchange (.ase) file and converts it into a list of colors and palettes. The output is a list that can contain both individual swatches and color groups (palettes). ```python import swatch # Assuming 'example.ase' exists in the same directory parsed_data = swatch.parse("example.ase") print(parsed_data) ``` -------------------------------- ### Write Adobe Swatch Exchange File Source: https://github.com/nsfmc/swatch/blob/master/README.rst Writes a list of colors and palettes to an Adobe Swatch Exchange (.ase) file. This function was introduced in version 0.4.0. ```python import swatch # Example data structure (replace with your actual data) color_list = [ { 'name': u'My Red', 'type': u'Process', 'data': {'mode': u'RGB', 'values': [1.0, 0.0, 0.0]} }, { 'name': u'My Palette', 'type': u'Color Group', 'swatches': [ {'name': u'Blue', 'type': u'Process', 'data': {'mode': u'RGB', 'values': [0.0, 0.0, 1.0]}} ] } ] output_filename = "output.ase" swatch.write(color_list, output_filename) print(f"Successfully wrote to {output_filename}") ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.