|
| 1 | +# Angular Color to Name Converter Example |
| 2 | + |
| 3 | +This example demonstrates how to integrate the **color-2-name** library with an Angular application to convert hex color codes to their closest color names. |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +- Convert hex colors (like `#F00`, `#00F`) to their closest color names |
| 8 | +- Interactive color input with real-time conversion |
| 9 | +- Pre-defined color buttons for quick testing |
| 10 | +- Beautiful, responsive UI with modern design |
| 11 | +- Error handling for invalid color formats |
| 12 | + |
| 13 | +## Live Demo |
| 14 | + |
| 15 | +The app converts: |
| 16 | +- `#F00` → `red` |
| 17 | +- `#00F` → `blue` |
| 18 | +- And any other valid hex color to its closest color name |
| 19 | + |
| 20 | +## Setup Instructions |
| 21 | + |
| 22 | +### Prerequisites |
| 23 | + |
| 24 | +- Node.js (v14 or higher) |
| 25 | +- npm or yarn |
| 26 | + |
| 27 | +### Installation |
| 28 | + |
| 29 | +1. Navigate to the Angular example directory: |
| 30 | + ```bash |
| 31 | + cd examples/angular |
| 32 | + ``` |
| 33 | + |
| 34 | +2. Install dependencies: |
| 35 | + ```bash |
| 36 | + npm install |
| 37 | + ``` |
| 38 | + |
| 39 | +### Running the Application |
| 40 | + |
| 41 | +1. Start the development server: |
| 42 | + ```bash |
| 43 | + npm start |
| 44 | + ``` |
| 45 | + |
| 46 | +2. Open your browser and navigate to: |
| 47 | + ``` |
| 48 | + http://localhost:4200 |
| 49 | + ``` |
| 50 | + |
| 51 | +### Building for Production |
| 52 | + |
| 53 | +1. Build the application: |
| 54 | + ```bash |
| 55 | + npm run build |
| 56 | + ``` |
| 57 | + |
| 58 | +2. The built files will be in the `dist/` directory. |
| 59 | + |
| 60 | +## Project Structure |
| 61 | + |
| 62 | +``` |
| 63 | +src/ |
| 64 | +├── app/ |
| 65 | +│ ├── app.component.ts # Main app component |
| 66 | +│ ├── app.component.html # App template |
| 67 | +│ ├── app.component.css # App styles |
| 68 | +│ ├── color-converter.component.ts # Color converter logic |
| 69 | +│ ├── color-converter.component.html # Converter UI |
| 70 | +│ └── color-converter.component.css # Converter styles |
| 71 | +├── index.html # Main HTML file |
| 72 | +├── main.ts # Angular bootstrap |
| 73 | +└── styles.css # Global styles |
| 74 | +``` |
| 75 | + |
| 76 | +## Key Implementation Details |
| 77 | + |
| 78 | +### Color Conversion Logic |
| 79 | + |
| 80 | +The color conversion is handled by the `ColorConverterComponent`: |
| 81 | + |
| 82 | +```typescript |
| 83 | +import { closest } from 'color-2-name'; |
| 84 | + |
| 85 | +convertColor() { |
| 86 | + try { |
| 87 | + const result = closest(this.colorInput); |
| 88 | + this.colorResult = { |
| 89 | + hex: this.colorInput, |
| 90 | + name: result.name, |
| 91 | + rgb: result.color |
| 92 | + }; |
| 93 | + } catch (err) { |
| 94 | + this.error = `Invalid color format: ${this.colorInput}`; |
| 95 | + } |
| 96 | +} |
| 97 | +``` |
| 98 | + |
| 99 | +### Angular Integration |
| 100 | + |
| 101 | +- Uses **Standalone Components** (Angular 17+) |
| 102 | +- Implements **Two-Way Data Binding** with `[(ngModel)]` |
| 103 | +- Includes **Form Handling** with `FormsModule` |
| 104 | +- Features **Responsive Design** with CSS Grid and Flexbox |
| 105 | + |
| 106 | +### Dependencies |
| 107 | + |
| 108 | +The key dependency is the `color-2-name` library: |
| 109 | + |
| 110 | +```json |
| 111 | +{ |
| 112 | + "dependencies": { |
| 113 | + "color-2-name": "^1.5.0", |
| 114 | + "@angular/core": "^17.0.0", |
| 115 | + // ... other Angular dependencies |
| 116 | + } |
| 117 | +} |
| 118 | +``` |
| 119 | + |
| 120 | +## Usage Examples |
| 121 | + |
| 122 | +### Basic Usage |
| 123 | + |
| 124 | +1. Enter a hex color in the input field (e.g., `#F00`, `#FF0000`) |
| 125 | +2. Press Enter or click "Convert" |
| 126 | +3. View the result showing the color name and RGB values |
| 127 | + |
| 128 | +### Quick Testing |
| 129 | + |
| 130 | +Click the pre-defined color buttons: |
| 131 | +- **Red** (`#F00`) → Converts to "red" |
| 132 | +- **Blue** (`#00F`) → Converts to "blue" |
| 133 | +- **Green** (`#0F0`) → Converts to "green" |
| 134 | +- And more... |
| 135 | + |
| 136 | +## Customization |
| 137 | + |
| 138 | +### Adding New Colors |
| 139 | + |
| 140 | +To add more pre-defined colors, update the `predefinedColors` array in `color-converter.component.ts`: |
| 141 | + |
| 142 | +```typescript |
| 143 | +predefinedColors = [ |
| 144 | + { hex: '#F00', name: 'Red' }, |
| 145 | + { hex: '#00F', name: 'Blue' }, |
| 146 | + // Add new colors here |
| 147 | + { hex: '#800080', name: 'Purple' }, |
| 148 | +]; |
| 149 | +``` |
| 150 | + |
| 151 | +### Styling |
| 152 | + |
| 153 | +Modify the CSS files to customize the appearance: |
| 154 | +- `color-converter.component.css` - Component-specific styles |
| 155 | +- `app.component.css` - Layout and container styles |
| 156 | +- `styles.css` - Global styles |
| 157 | + |
| 158 | +## Troubleshooting |
| 159 | + |
| 160 | +### Common Issues |
| 161 | + |
| 162 | +1. **Build Errors**: Ensure all dependencies are installed with `npm install` |
| 163 | +2. **Port Already in Use**: The app uses port 4200 by default. Kill existing processes or use a different port |
| 164 | +3. **Color Conversion Errors**: Make sure to enter valid hex color formats (#RGB, #RRGGBB) |
| 165 | + |
| 166 | +### Getting Help |
| 167 | + |
| 168 | +- Check the [color-2-name documentation](https://github.com/wp-blocks/color-2-name) |
| 169 | +- Review Angular's [official documentation](https://angular.io/docs) |
| 170 | + |
| 171 | +## License |
| 172 | + |
| 173 | +This example follows the same license as the color-2-name library (ISC). |
0 commit comments